NotJay
NotJay

Reputation: 4076

Take an array of id numbers and gather info from another table in alphabetical order

I am trying to take an array list of product ids, get the associated data (which is stored in another table, I know... that is poorly set up), and then alphabetize them based on the product name. Here is what I have thus far:

$set = array($aID['id']);
$getInfo = mysql_query("SELECT * FROM tbl WHERE FIND_IN_SET('$set', id) ORDER BY name ASC");
while($product = mysql_fetch_array($getInfo))
    {
        echo $product['name'] . " <br /\n";
    }

I am getting an error message for the while row which means something is wrong on my $getInfo query line. What am I doing wrong? Any help and all constructive criticism is appreciated.

Upvotes: 0

Views: 92

Answers (3)

GBD
GBD

Reputation: 15981

You can also use IN

$ids = implode(",",$set);
$getInfo = mysql_query("SELECT * FROM tbl WHERE id IN (".$id.") ORDER BY name ASC");

Upvotes: 0

magarisi
magarisi

Reputation: 757

I think that it takes $set as part of mysql query. You should show that it is an external variable from php like this:

$getInfo = mysql_query("SELECT * FROM tbl WHERE FIND_IN_SET('.$set.', id) ORDER BY name ASC");

Upvotes: 0

Ray
Ray

Reputation: 41428

You can't pass an array into mysql. You need to convert it to a comma seperated string:

$set = array($aID['id']);
$set = implode(',' $set);

Also the order is reversed in your FIND_IN_SET. It should be

FIND_IN_SET(id, '$set')

Honestly, I'd just use IN as FIND_IN_SET returns an index of where it is in the set, not probably what you want:

SELECT * FROM tbl WHERE id IN ($set) ORDER BY name ASC

Upvotes: 3

Related Questions