Jump to content


Photo

mysql query and array values


  • Please log in to reply
2 replies to this topic

#1 s2day

s2day
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 May 2006 - 11:22 AM

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

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)
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());

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.

#2 roxii

roxii
  • Members
  • Pip
  • Newbie
  • 6 posts
  • LocationNorway

Posted 19 May 2006 - 11:30 AM

It's not the cleanest code, but I think this is what you're looking for.

<?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);
?>


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 May 2006 - 12:10 AM

try

$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());

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users