master82 Posted June 8, 2007 Share Posted June 8, 2007 I need to create an array from a list of IDs within a database table. The table consists of 3 fileds, a unique ID, a user ID and the last is an int value (item number) eg 1 3 7 2 4 5 3 3 2 etc... How would I go about using this query $query1=mysql_query("SELECT itemnumber FROM table WHERE userid = '{$user}'"); Would i be right in thinking I can use: $array = mysql_fetch_array($query1); to store all the item numbers associated with the user specified in the first query? Quote Link to comment https://forums.phpfreaks.com/topic/54811-creating-an-array/ Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 while ($row = mysql_fetch_array($query1)) { $array[] = $row; } print_r($array); Quote Link to comment https://forums.phpfreaks.com/topic/54811-creating-an-array/#findComment-271063 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Share Posted June 8, 2007 $sql = "SELECT itemnumber FROM table WHERE userid = '{$user}'"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $itemnumber[] = $row['itemnumber']; } }else{ echo 'not found'; } }else{ echo mysql_error(); } } Quote Link to comment https://forums.phpfreaks.com/topic/54811-creating-an-array/#findComment-271065 Share on other sites More sharing options...
master82 Posted June 8, 2007 Author Share Posted June 8, 2007 Would they allow me to place the array into another select query (in the where part) later on, or cant you place an array into such queries? Quote Link to comment https://forums.phpfreaks.com/topic/54811-creating-an-array/#findComment-271077 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Share Posted June 8, 2007 for that you'd need to echo out the array using a foreach statement. for example: $sql = "SELECT "; foreach($array as $key => $value){ $sql .= $value . ", "; } $sql .= "FROM table WHERE userid = '{$user}'"; That's an example. It wont work completely, because you'll have a ',' just before FROM which wont work. You'd have to check if it's the last one in the array and if so NOT output the ',' Quote Link to comment https://forums.phpfreaks.com/topic/54811-creating-an-array/#findComment-271084 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.