The_Maclover Posted April 12, 2011 Share Posted April 12, 2011 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! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/ Share on other sites More sharing options...
requinix Posted April 12, 2011 Share Posted April 12, 2011 Those last couple sentences confused me so I'm ignoring them. Have you looked at $arr? How does that compare to what you want? Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200858 Share on other sites More sharing options...
The_Maclover Posted April 12, 2011 Author Share Posted April 12, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200861 Share on other sites More sharing options...
requinix Posted April 12, 2011 Share Posted April 12, 2011 That could very well be the value of $arr2. I'm talking about $arr. The other variable. Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200893 Share on other sites More sharing options...
The_Maclover Posted April 13, 2011 Author Share Posted April 13, 2011 well. I took the script on a forum where (i thought) showed me what i wanted to do, I think $arr is created for every records... no? Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200909 Share on other sites More sharing options...
gizmola Posted April 13, 2011 Share Posted April 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200916 Share on other sites More sharing options...
The_Maclover Posted April 13, 2011 Author Share Posted April 13, 2011 can the indexed numbering be determinate by an ID field in the database? Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200934 Share on other sites More sharing options...
gizmola Posted April 13, 2011 Share Posted April 13, 2011 Yes. $arr = array(); while ($row[] = mysql_fetch_assoc($data)) { $arr[$row['id']] = $row; } var_dump($arr); Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1200952 Share on other sites More sharing options...
The_Maclover Posted April 13, 2011 Author Share Posted April 13, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1201329 Share on other sites More sharing options...
requinix Posted April 13, 2011 Share Posted April 13, 2011 while ($row = mysql_fetch_array($data)) { No []s there. Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1201339 Share on other sites More sharing options...
The_Maclover Posted April 14, 2011 Author Share Posted April 14, 2011 Where do you want me to remove []s ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1201344 Share on other sites More sharing options...
gizmola Posted April 14, 2011 Share Posted April 14, 2011 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 ' '; } Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1201354 Share on other sites More sharing options...
The_Maclover Posted April 14, 2011 Author Share Posted April 14, 2011 YEAH!!! it's working now , 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 A HUGE thank you guys, you really helped me Quote Link to comment https://forums.phpfreaks.com/topic/233543-fetch-mysql-and-assign-it-to-array/#findComment-1201397 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.