tomcat01 Posted February 5, 2007 Share Posted February 5, 2007 Hi all Need some help with the following please: I want to fetch data from mysql but the id is part of an array $ids = array("1","2","3"); $sql = mysql_query("SELECT * FROM table WHERE id ='$ids'") or die ("Error". mysql_error() ); This gives an obvious error but how can I get it to return the rows having ids 1 ,2 and 3 only! - It must not give me ids 5, 101 or 5 aswell should they exist. The array can be 20 values so I cant build it into the select. Hope you understand what I'm looking for. Please help me out on this one.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/37127-php-mysql-fetch-array/ Share on other sites More sharing options...
worldworld Posted February 12, 2007 Share Posted February 12, 2007 You must have for loop before $sql.... Quote Link to comment https://forums.phpfreaks.com/topic/37127-php-mysql-fetch-array/#findComment-182712 Share on other sites More sharing options...
xec Posted February 17, 2007 Share Posted February 17, 2007 Hii, use this code $ids = array("1","2","3"); foreach($ids as $i) { $sql = mysql_query("SELECT * FROM table WHERE id ='$i'") or die ("Error". mysql_error() ); $res[] = mysql_fetch_array($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/37127-php-mysql-fetch-array/#findComment-186955 Share on other sites More sharing options...
kalai Posted February 21, 2007 Share Posted February 21, 2007 Hi , Instead of running the loop many times, Keep your values in a string , $ids = "1,2,3"; $sql = mysql_query("select * from table where id in (".$ids.")"); $data [] = mysql_fetch_array($sql); Quote Link to comment https://forums.phpfreaks.com/topic/37127-php-mysql-fetch-array/#findComment-190591 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.