Jump to content

Archived

This topic is now archived and is closed to further replies.

s2day

mysql query and array values

Recommended Posts

Hi,
I have an array:
[code]Array ( [0] => 122431206 [1] => 122431200 [2] => 210998761 [3] => 122431203 ) [/code]

Now, I want to write a select query that would not select a row when a particular field's value equals any of those in the array above:
($ssna is the array)
[code]
mysql_query("SELECT * FROM `_population` WHERE `school_id`='$sch' OR `work_id`='$wor' OR `residence_id`='$res'  HAVING `ssn`!='$ssna' ORDER BY `lname` ASC") or die(mysql_error());[/code]

The above query gives results which include rows containing the values in the array...

how can I make the query not select the rows corresponding to the array values?

Thank you.

Share this post


Link to post
Share on other sites
It's not the cleanest code, but I think this is what you're looking for.

[code]
<?php
$array = array(122323, 23242, 23524);
$ids = null;
foreach($array as $match_id) {
    $ids = $ids."id!='".$match_id."' OR ";
}
$ids = rtrim($ids, " OR ");

$query = mysql_query("SELECT * FROM table WHERE ".$ids);
?>
[/code]

Share this post


Link to post
Share on other sites
try

[code]$ssna = array ( 122431206, 122431200, 210998761, 122431203 );
$ssnList = join (',', $ssna);
mysql_query("SELECT * FROM `_population`
            WHERE (
                `school_id`='$sch'
                OR `work_id`='$wor'
                OR `residence_id`='$res'
                )  
                AND `ssn` NOT IN ($ssnList)
            ORDER BY `lname` ASC") or die(mysql_error());[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.