Q695 Posted October 2, 2010 Share Posted October 2, 2010 What am I missing to do the search? error test: Array ( [0] => [1] => 1 ) Array ( [0] => [1] => 1 [2] => 4 ) sql error sql : SELECT * FROM thoughts WHILE id = 'Array' ORDER BY `thoughts`.`time` DESC You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE id = 'Array' ORDER BY `thoughts`.`time` DESC' at line 1 <?php $sql2="SELECT * FROM thoughts WHILE id = '$contacts' ORDER BY `thoughts`.`time` DESC"; $result=@mysql_query($sql2,$con) or die(death($sql2)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/ Share on other sites More sharing options...
DavidAM Posted October 2, 2010 Share Posted October 2, 2010 I can't really tell what your $contacts array looks like; but if it is a simple array of numeric id's you can just implode() it: $sql2="SELECT * FROM thoughts WHERE id IN (" . implode(', ', $contacts) . ") ORDER BY `thoughts`.`time` DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/#findComment-1118385 Share on other sites More sharing options...
Q695 Posted October 2, 2010 Author Share Posted October 2, 2010 new error: Array ( [0] => [1] => 1 ) Array ( [0] => [1] => 1 [2] => 4 ) sql error sql : SELECT * FROM thoughts WHERE id IN (, 1, 4) ORDER BY `thoughts`.`time` DESC You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 1, 4) ORDER BY `thoughts`.`time` DESC' at line 1 My array loader code is: $contacts = array(""); while ($row=mysql_fetch_array($result)){ if ($id==$row[user_1]){ $contact=$row[user_2]; } else { $contact=$row[user_1]; } array_push($contacts, "$contact"); print_r($contacts); } Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/#findComment-1118386 Share on other sites More sharing options...
BlueSkyIS Posted October 2, 2010 Share Posted October 2, 2010 IN (, 1, 4) is an invalid IN statement. Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/#findComment-1118396 Share on other sites More sharing options...
Q695 Posted October 2, 2010 Author Share Posted October 2, 2010 I know, but what what is the code solution to build an array out of filter picked solutions? I can put 0, due to the auto-increment never having that be a number, but if the website takes off, I want something that will be quicker than checking if something is always going to be a 0. It could put your own posts up. Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/#findComment-1118399 Share on other sites More sharing options...
BlueSkyIS Posted October 2, 2010 Share Posted October 2, 2010 implode() is correct, but apparently one of the values for $contact is an empty string, so the query fails. here is how I use implode to account for this possibility. i ALWAYS single-quote values, be they numeric or non-numeric. $sql2="SELECT * FROM thoughts WHERE id IN ('" . implode("', '", $contacts) . "') ORDER BY `thoughts`.`time` DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/#findComment-1118401 Share on other sites More sharing options...
Q695 Posted October 2, 2010 Author Share Posted October 2, 2010 I guess I should've been using a where, not while . Quote Link to comment https://forums.phpfreaks.com/topic/214999-pulling-data-on-mysql-using-an-array/#findComment-1118405 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.