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

Link to comment
Share on other sites

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

Link to comment
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;

 

thanks, I just made one like you did with $output , but the double array was confusing me

Link to comment
Share on other sites

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

Link to comment
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;

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.