Moody Dude Posted June 10, 2011 Share Posted June 10, 2011 Hi, I am pulling in 3 sets of data from a mysql database and echoing to the screen (the data is to be used to plot a graph); what I need to know is how do I store each set of values in 3 separate arrays for use later in the program. The current code is shown below. Thanks in advance MD $query=mysql_query("SELECT * FROM report WHERE report.reportid=$value"); while ($row=mysql_fetch_array($query)) { for($x=1; $x<11; $x++) { echo $row[$x]; } } Quote Link to comment https://forums.phpfreaks.com/topic/238975-php-arrays/ Share on other sites More sharing options...
Muddy_Funster Posted June 10, 2011 Share Posted June 10, 2011 $row[] is an array.... and why are you running a loop within a loop Quote Link to comment https://forums.phpfreaks.com/topic/238975-php-arrays/#findComment-1227949 Share on other sites More sharing options...
fugix Posted June 10, 2011 Share Posted June 10, 2011 mysql_fetch_array() is already placing your column values into an array, to draw a val out all you need to do is echo $row['column_name'] Quote Link to comment https://forums.phpfreaks.com/topic/238975-php-arrays/#findComment-1227950 Share on other sites More sharing options...
Adam Posted June 10, 2011 Share Posted June 10, 2011 You can use a multi-dimensional array to keep them altogether, while in separate arrays: $data = array(); while ($row=mysql_fetch_array($query)) { $data[] = $row; } The only small problem with this, is that you're using mysql_fetch_array, which returns the data indexed by both column name and number -- so your array would be twice the size it needs to be. To prevent this you should use either mysql_fetch_row (for numeric array) or mysql_fetch_assoc (for associative array). Quote Link to comment https://forums.phpfreaks.com/topic/238975-php-arrays/#findComment-1227987 Share on other sites More sharing options...
Moody Dude Posted June 10, 2011 Author Share Posted June 10, 2011 Hi Thanks for the replies. The first loop was to select the correct rows within the table and the second loop was to echo the values of each field of the selected rows to the screen. Not sure if this is programmatically correct or not but as a novice it seemed logical!! Will try and solve this over the weekend and report back. Thanks again, MD Quote Link to comment https://forums.phpfreaks.com/topic/238975-php-arrays/#findComment-1228003 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.