Jump to content

Fetch MySQL and assign it to Array??


The_Maclover

Recommended Posts

Hi everyone! thank for reading my post, I really ned help  :'(

Basically, I want to fetch data form my MySQL db (every records) and assign it to an array, something like that:


$dbhost = 'myhost';
$dbuser = 'user';
$dbpass = 'password

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to database');
$dbname = 'db';
mysql_select_db($dbname);
$data = mysql_query("SELECT * FROM photohome") 	or die(mysql_error()); 

 while ($record = mysql_fetch_assoc($data)) {
 $arr[] = array($record['name'],$record['amount'],$record['archive']);
 $arr2[] = implode(',',$arr);
 }
 $cities = implode('<br/>',$arr);
 print_r($arr2);



mysql_close($conn);

but it doesn't output what I want... I want to have something like that :

// product[number] = array('Name' ,'Price' , 'Download Link'); 

$products[2] = array('My Script','32.00','downloads/myscript.zip');

but the product number will change based on the id of the record in the table. The name, price and link would change too, based on the product id...

i'm sure i'm in the wrong way... please help me! :D

Thank you :)

Link to comment
Share on other sites

the output i get is :

Array ( [0] => Array [1] => Array,Array )

but i want to have an output (preferably in a variable) that is the same structure as my 2nd example, where the [number] (of $product) change dynamically(a unique number for every record) and where the entries in the $product array change dynamically too

hope it's more clear... :)

Link to comment
Share on other sites

When you call mysql_fetch_assoc() you already get an associative array for that one row.  For example:

 


$arr = array();

while ($arr[] = mysql_fetch_assoc($data)) {
  // not sure what you need here
}

var_dump($arr);

 

If you only want certain columns, you can specify them in your SELECT statement using SELECT column1, column2, column3... etc rather than SELECT *.

 

At this point I'm not really clear on what you are trying to do or why, but $arr will be a numerically indexed array (0 based) where each element is an array that is equivalent to a row in the result set.

 

Link to comment
Share on other sites

Thank you for your reply :)

unfortunately, it's not working the way i want it, why my code (based on your's) won't work? :


while ($row[] = mysql_fetch_array($data)) {
   $arr[$row['id']] = array($row['name'], $row['amount']);
    var_dump($arr);
    echo '<br/><br/>';
}
echo $arr['3473'];

I think it's pretty self-explanatory, 3473 is the ID of my record in the db

That'S the output:

array(1) { [""]=> array(2) { [0]=> NULL [1]=> NULL } } 

 

thank you! :D

Link to comment
Share on other sites

Yeah I had a bug in there.. i just type this stuff in and don't really test it usually.  Do exactly what requinix wrote in his reply for the outer while loop.

 


while ($row = mysql_fetch_array($data)) {
   $arr[$row['id']] = array($row['name'], $row['amount']);
    var_dump($arr);
    echo '

';
}

 

 

Link to comment
Share on other sites

YEAH!!! it's working now :D , I removed the [] after $row in the "while" and changed mysql_fetch_array for mysql_fetch_assoc, here's my code :

$data = mysql_query("SELECT * FROM photohome") 	or die(mysql_error()); 
while ($row = mysql_fetch_assoc($data)) {
   $arr[$row['ide']] = array($row['name'], $row['amount']);
    var_dump($arr);
    echo '<br/><br/>';
}
var_dump($arr['IMG_3473']);

and it's outputting the right things :D

A HUGE thank you guys, you really helped me :D

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.