andrej13 Posted March 9, 2011 Share Posted March 9, 2011 Hey guys, I have made an array where only the name shows up, but I want the price next to it. I adapted the query, but how do I need to adapt the array? peace <?php // connect to your database $DrinkArray=array() ; $DrinkResult=mysql_query("SELECT name,price FROM products")or die(mysql_error()); while($DrinkRow=mysql_fetch_assoc($DrinkResult)){ $DrinkArray[]=$DrinkRow['name']; } //check your output with something like this foreach($DrinkArray as $value){ echo $value.'<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/ Share on other sites More sharing options...
Psycho Posted March 9, 2011 Share Posted March 9, 2011 Why are you looping through the db results to put them in an array just so you can then loop through the array to display the output? Makes no sense. Just create the output while you process the db results $query = "SELECT name, price FROM products"; $result = mysql_query($query)or die(mysql_error()); $ouput = ''; while($DrinkRow = mysql_fetch_assoc($result)) { $output .= "{$DrinkRow['name']} : {$DrinkRow['price']}<br />\n"; } echo $output; Quote Link to comment https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/#findComment-1185059 Share on other sites More sharing options...
xjasonx Posted March 9, 2011 Share Posted March 9, 2011 <?php // connect to your database $DrinkArray=array() ; $DrinkResult=mysql_query("SELECT name,price FROM products")or die(mysql_error()); while($DrinkRow=mysql_fetch_assoc($DrinkResult)){ $DrinkArray[]= array('name' => $DrinkRow['name'], 'price' => $DrinkRow['price']); } return $DrinkArray; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/#findComment-1185060 Share on other sites More sharing options...
andrej13 Posted March 9, 2011 Author Share Posted March 9, 2011 Why are you looping through the db results to put them in an array just so you can then loop through the array to display the output? Makes no sense. Just create the output while you process the db results $query = "SELECT name, price FROM products"; $result = mysql_query($query)or die(mysql_error()); $ouput = ''; while($DrinkRow = mysql_fetch_assoc($result)) { $output .= "{$DrinkRow['name']} : {$DrinkRow['price']}<br />\n"; } echo $output; thanks, I just made one like you did with $output , but the double array was confusing me Quote Link to comment https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/#findComment-1185061 Share on other sites More sharing options...
andrej13 Posted March 9, 2011 Author Share Posted March 9, 2011 <?php // connect to your database $DrinkArray=array() ; $DrinkResult=mysql_query("SELECT name,price FROM products")or die(mysql_error()); while($DrinkRow=mysql_fetch_assoc($DrinkResult)){ $DrinkArray[]= array('name' => $DrinkRow['name'], 'price' => $DrinkRow['price']); } return $DrinkArray; ?> This does not work for me :s Quote Link to comment https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/#findComment-1185067 Share on other sites More sharing options...
Psycho Posted March 9, 2011 Share Posted March 9, 2011 Why are you looping through the db results to put them in an array just so you can then loop through the array to display the output? Makes no sense. Just create the output while you process the db results $query = "SELECT name, price FROM products"; $result = mysql_query($query)or die(mysql_error()); $ouput = ''; while($DrinkRow = mysql_fetch_assoc($result)) { $output .= "{$DrinkRow['name']} : {$DrinkRow['price']}<br />\n"; } echo $output; thanks, I just made one like you did with $output , but the double array was confusing me What "double" array? That code is creatng a string in the $ouput variable from the DB results. As I stated before it is dumb to dump the DB results into an array just so you can loop over the array to create the output. It is two processes when you only need one. Quote Link to comment https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/#findComment-1185070 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.