how to convert strings into arrays using php


Problem :

I have a stored string that echoes like this

$string = 24,56,43,23,45;

I want to retrieve data from mysql, so I do this;

$getcompany = mysql_query("SELECT * FROM pages WHERE id = $string");

If the id matches any of the strings, it should show that info but nothing happens. I tried using explode to breakdown the strings but I don't know how to make the query loop through the string to display the info that matches the string.

I hope am making sense to someone :( pls help.



Solution :

use IN:

$string = "24,56,43,23,45";
$getcompany = mysql_query("SELECT * FROM pages WHERE id IN (" . $string . ")");

Which would translate in to the query:

SELECT *
FROM   pages
WHERE  id IN (24,56,43,23,45);

As long as id is a numeric field that will work. If id is a string, you can rework it a bit:

$string = "24,56,43,23,45";
$args = "'" . implode("','", explode(",", $string)) . "'";
$getcompany = mysql_query("SELECT * FROM pages WHERE id IN (" . $args . ")");

Which would translate to:

SELECT *
FROM   pages
WHERE  id IN ('24','56','43','23','45');

Mysql Tutorials

Mysql Howto..

How to append a value to a MySQL query

How to select the COUNT() of a joined table when the joined table has a combination of multiple columns as its primary key?

How to insert data into mysql using Java

How to install PhpMyAdmin having MySQL database/logins through FTP

Joomla database how to do WHERE AND OR

How to revoke explain plan privilege in mysql for a user? is it possible

POSTGRE how to select parent name from hierarchical table

How can I download a MySQL's BLOB using Servlets and AJAX?

How to get exact search result using MYSQL LIKE operator

preventing £ from being echoed: why doesn't the actual symbol show?

How to identify clusters of nodes in a network

How to add or plus times to NOW()

how to display a blob type in struts2 jquery grid from mysql database?

How to add Foreign Key (MySQL)

How to create a sql file from MySQL command line client

How do I account for missing xPaths and keep my data uniform when scraping a website using DOMXPath query method?

How to multiply row from mysql by quantity from form?

MySQL blob: how to get just a subset of the stored data

how to select update in the same table in mysql?

How to organize MySQL table for multi player game Win/Loss reports

how to retrieve and output images from multiple columns with php and mysql?

How to enable users from another domain to search records from my domain?

How to display fetched result inside php form?

How do I establish a connection to MySQL via VB & display simple query results on a form?

How to check the existence of mysql database? [duplicate]

How to quickly develop a sortable, paged data grid of a MySQL table in PHP? [closed]

How to use two values to define order in MySQL but priorize one of them?

How to get a list of MySQL views?

How to insert data to mysql through a modal

how to update sql table with a variable number of fields in the form php