Jump to content

put sql columns in array


andrej13

Recommended Posts

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 />';
      }
?>

 

Link to comment
https://forums.phpfreaks.com/topic/230106-put-sql-columns-in-array/
Share on other sites

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;

<?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;
?>

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

<?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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.