marcus Posted December 31, 2006 Share Posted December 31, 2006 How would I make an array out of a while statement[code]$sql = "SELECT * FROM `list_items` WHERE `type` ='food'";$res = mysql_query($sql) or die(mysql_error());while($row = mysql_fetch_assoc($res)){$row[item_id];}[/code]and take the while statement stuff and put it into an array Quote Link to comment Share on other sites More sharing options...
mkosmosports Posted December 31, 2006 Share Posted December 31, 2006 I think this would do it:$sql = "SELECT * FROM `list_items` WHERE `type` ='food'";$res = mysql_query($sql) or die(mysql_error());while($row = mysql_fetch_assoc($res)){$nameofarray[] = $row[item_id];} Quote Link to comment Share on other sites More sharing options...
marcus Posted December 31, 2006 Author Share Posted December 31, 2006 That would make it into an array as in array(); ? Quote Link to comment Share on other sites More sharing options...
trq Posted December 31, 2006 Share Posted December 31, 2006 [code=php:0]$sql = "SELECT * FROM `list_items` WHERE `type` ='food'";$res = mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($res)) { $array[] = $row['item_id'];}[/code] Quote Link to comment Share on other sites More sharing options...
marcus Posted December 31, 2006 Author Share Posted December 31, 2006 Fatal error: Cannot use [] for reading in /home/kael/public_html/rs_cron/food.php on line 24 Quote Link to comment Share on other sites More sharing options...
marcus Posted December 31, 2006 Author Share Posted December 31, 2006 That happens when I try to echo it off. Quote Link to comment Share on other sites More sharing options...
trq Posted December 31, 2006 Share Posted December 31, 2006 You cant simply echo an array. You need to use a loop. Quote Link to comment Share on other sites More sharing options...
marcus Posted December 31, 2006 Author Share Posted December 31, 2006 How would I make it into the array? I don't understand this. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 31, 2006 Share Posted December 31, 2006 try this - same as thorpe's $sql = "SELECT * FROM `list_items` WHERE `type` ='food'";$res = mysql_query($sql) or die(mysql_error());$array = array();while ($row = mysql_fetch_assoc($res)) { $array[] = $row['item_id'];}print_r($array); Quote Link to comment Share on other sites More sharing options...
trq Posted December 31, 2006 Share Posted December 31, 2006 This will make an array called $array out of all the rows selected from your database.[code=php:0]$sql = "SELECT * FROM `list_items` WHERE `type` ='food'";$res = mysql_query($sql) or die(mysql_error());while ($row = mysql_fetch_assoc($res)) { $array[] = $row['item_id'];}[/code]Then, to display this array you'll need a loop, something like.[code=php:0]foreach($array as $val) { echo $val;}[/code]As you may have noticed, making an array out of a database record like you asked is next to useless. Its more work for both you and php to create two loops. Quote Link to comment 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.