Jump to content

HELP for Fetching MySQL table in array!!!


The_Maclover

Recommended Posts

Hi everyone! i'm freshly new here and I have a big nightmare:

What i want to do is separated in 3 phases:

1. Fetch every Records for MySQL Table ;

2. For each Records, create a variable for it( or something like that, dynamically, e.g.: data[something that change automatically for every records]);

3. Do something so that for each Records i can output fields e.g.: data[name];

Is it clear? here's my script do far, it's only doing part 1 and looping for each records but i can't store them in variable...

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to database');
$dbname = 'u158274072_db';
mysql_select_db($dbname);
$data = mysql_query("SELECT * FROM photohome") 	or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) 
{ 
$name[] = $info['name']."<br/>"; 
$amount[] = $info['amount'] . " <br>"; 
} 
print_r($name);

But the output of $name is only the first record of the table. if i'm doing

echo $info['name']."<br/>"; 

instead of

$name[] = $info['name']."<br/>"; 

it output every records but i can't store them in a variable..

Thanks for all you help!

Link to comment
https://forums.phpfreaks.com/topic/232570-help-for-fetching-mysql-table-in-array/
Share on other sites

Change this:

while($info = mysql_fetch_array( $data )) 
{ 
$name[] = $info['name']."<br/>"; 
$amount[] = $info['amount'] . " <br>"; 
} 
print_r($name);

 

To this:

 

while($info = mysql_fetch_array( $data )) 
{ 
$name = $info[0];
$amount = $info[1];
} 

echo $name. " <br>"; 
echo $amount. " <br>"; 

 

You can also use $info['name'] like you have it.

Hi

 

Your code looks about right but not sure what you are expecting it to do.

 

You are storing each rows name as a member of an array (assuming $name is initialised as an array somewhere). You will thus have an array with all the values in it. Same for amount.

 

However I am not sure from the description what exactly is the problem you are having with the code.

 

All the best

 

Keith

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.