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 Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/ 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];} Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150161 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(); ? Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150162 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] Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150163 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 Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150164 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. Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150165 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. Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150173 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. Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150175 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); Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150176 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. Link to comment https://forums.phpfreaks.com/topic/32341-making-an-array-out-of-a-while-statement/#findComment-150177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.