tags:

views:

79

answers:

5

Hi ,I'm trying to build a query using php and mysql,

 $query = "select * from products where product_name = '$item_name'";

this works when $item_name holds only one name, but $item_name is an array and based on the user's interaction can contain multiple names, how can I make the query to run for multiple name and get the resulted rows.

Thanks in advance

+3  A: 
$vals = implode(',',$item_name);
$query = "select * from products where product_name in (".$vals.");";

Give that a try.

inkedmn
should be $vals = '"'.implode('","', $item_name).'"'; in order to have quoted values
Eineki
+1, but yes, dove, is right sanitizing data would be very helpful.
Michael Krelin - hacker
this isn't a good example of how to construct a query - santizing the data should never be afterthought, or even removed from instructive examples to make them clearer.
Paul Dixon
thanks for you reply,after running the above code I get this errorWarning: implode() [function.implode]: Invalid arguments passedany idea,
amir
amir, what's in your `$item_name`? If it's, for instance, comma-separated list, you may want to `explode` it first. But go for Paul's reply.
Michael Krelin - hacker
A: 
foreach($item_name as $name) {
$query = "select * from products where product_name = '$name'";
//whatever you want to do with the query here
}

something like that ought to do it.

This is an inefficient solution as it requires a round-trip to the database for each element in the array. My solution gets them all in one shot.
inkedmn
Hm, true...silly answer :-p
A: 
$query = "select * from products where product_name in(";
foreach($item_name as $name) 
{
    $query .= "'" . $item_name . "', ";
}

$query = substr($query, 0, strlen$query) - 2);
$query .= ");";

First answer (by inkedmn) is really the best one though

Natrium
You have a great opportunity to escape the string here ;-) And close the ')' afterwards.
Michael Krelin - hacker
yeah, not the best answer in the list.
Natrium
+6  A: 

Here's how you could build a safe list of names for inserting into an IN clause...

if (is_array($names) && count($names))
{
    $filter="('".implode("','" array_map('mysql_real_escape_string', $names))."')";
    $sql="select * from products where product_name in $filter";

    //go fetch the results
}
else
{
    //input was empty or not an array - you might want to throw an
    //an error, or show 'no results'
}

array_map returns the input array of names after running each name through mysql_real_escape_string to sanitize it. We implode that array to make a nice list to use with an IN clause.

You should always ensure any data, particularly coming directly from the client side, is properly escaped in a query to prevent SQL injection attacks.

Paul Dixon
+1, finally a complete solution. ;-)
Michael Krelin - hacker
To make it even more instructive, I'd suggest an `else` clause to avoid surprises ;-)
Michael Krelin - hacker
good point, done!
Paul Dixon
A: 

Based on inkedmn's response (which didn't quote the item names):

$query = 'select * from products where product_name in ("' . implode('", "', $item_name ) . '")';

Although you may be better with a fulltext search.

http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

voidstate
too much double quotes
Natrium
Too many double quotes? What do you mean? It will become something like: 'select * from products where product_name in ("shoes", "shirts", "hats")'
voidstate