alicia233 alicia233 - 1 year ago 61
MySQL Question

Querying WHERE column is IN ($array) returns only one row

I'm trying to query all results where surveyid contains my array $subscibearray. The problem is the SQL query is only returning the first result (44 in this case) instead of all of them. I tried using mysql_fetch_array as well without any success. My results are being outputted via Table. Any help would be appreciated. I omitted as much code as possible to make this easy to read.

Array format:

$subscribearray = "" . join(', ',$subscribearray) . "";


string(17) "'44, 35, 194, 36'"


$result = mysql_query("SELECT *
FROM surveys
WHERE surveyid IN ($subscribearray)
ORDER BY peercompletetime DESC
LIMIT 100")
or die(mysql_error());

Answer Source

You should:

$subscribearray = join(',', $subscribearray); // 1,2,3


$subscribearray = "'" . join("','", $subscribearray) . "'"; // '1','2','3'

If you wrap all ids with a string, it will look for the string, and not separated values. ('1,2,3')

Of course, if it's a numeric column you should use the first one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download