The_Maclover Posted April 3, 2011 Share Posted April 3, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/232570-help-for-fetching-mysql-table-in-array/ Share on other sites More sharing options...
sunfighter Posted April 3, 2011 Share Posted April 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232570-help-for-fetching-mysql-table-in-array/#findComment-1196309 Share on other sites More sharing options...
The_Maclover Posted April 3, 2011 Author Share Posted April 3, 2011 and if i want to do something like this: $product[0] = array($name[0], $amount[0]); $product[1] = array($name[1], $amount[1]); ...etc until every records have an associated $product[] ??? thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/232570-help-for-fetching-mysql-table-in-array/#findComment-1196360 Share on other sites More sharing options...
kickstart Posted April 4, 2011 Share Posted April 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232570-help-for-fetching-mysql-table-in-array/#findComment-1196507 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.