Jump to content

mysql query and array values


s2day

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.
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.