Stack Exchange
PHP - mysql_query() issue with malformed WHERE clause
I've this string with fruit names together with available colours separated by ,. Multiple fruits details are separated using |.
$string = 'apple,red,green|banana,yellow,green';

In the below piece of code I extract only fruit name using explode(). And use fruit name to query records from MySQL DB.
foreach ( explode( '|', $string ) as $fruitas ) {
  $data = explode( ',', $fruitas );

  $sql = mysql_query( "SELECT * FROM user WHERE fruits = '$data[0]' ") or die ( mysql_error() );
  while ( $row = mysql_fetch_array( $sql ) ){
    $fruits = $row['fruits'];
    echo $fruits . '<br />';
  }
}

$data[0] has the name of the fruit.
I expected below output: 
APPLE    20
BANANA   20

Because apple and banana are fruit names in $string. And Query should pull records for apple and banana only.
I know my codes is correct, but when I echo() the pulled records, nothing prints!.
Please help me figure out the issue.
* UPDATE 
FOUND THE ANSWER*

why? i need to get the value from DB?

Put a var_dump($row); inside the loop, and var_dump($sql); just before the while loop.

hi i got this respond bool(true) bool(false) bool(true) bool(false) bool(true) bool(false) 

mysql_query is not recommended to use in our time, use mysqli or PDO

Change:
$fruits = $row[fruits];

to
$fruits = $row['fruits'];

and
echo $fruits . <br />

to
echo $fruits . '<br />';


This is not valid PHP and I think your code is not running at all:
echo $fruits . <br />

The proper way of debugging something like this is to first of all make your code parsable. Then find out if your query is what you expect it to be: change mysql_query() into print() and see if your query is correct. You might be querying values you're not expecting to query, and you can run the query in the MySQL console (or through phpMyAdmin) if available to see if it returns the correct value.
Then add a debug statement such as var_dump($row); in the loop, to see if you're actually getting anything out from the query, and then see if the value of $fruits is what you expect it to be.
Extra: You should drop mysql in future projects and use mysqli or PDO instead. They're both more modern database libraries that support much needed features such as placeholders.
To give a better answer you'll also have to include your database structure the next time.

sorry i know --i should make it clear, i just type it. anyway its still not working even i change the br tag
