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. Quote 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] Quote 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] Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.