s2day Posted May 19, 2006 Share Posted May 19, 2006 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 https://forums.phpfreaks.com/topic/9994-mysql-query-and-array-values/ Share on other sites More sharing options...
roxii Posted May 19, 2006 Share Posted May 19, 2006 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 https://forums.phpfreaks.com/topic/9994-mysql-query-and-array-values/#findComment-37130 Share on other sites More sharing options...
Barand Posted May 20, 2006 Share Posted May 20, 2006 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 https://forums.phpfreaks.com/topic/9994-mysql-query-and-array-values/#findComment-37324 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.