sk121506 Posted April 4, 2008 Share Posted April 4, 2008 im trying to make an array of multiple rows i am retrieving from a db. my logic is just somewhere else right now. any help would be great! $query = "SELECT Day,Time,TOD FROM January WHERE Username='$_SESSION[username]'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $array[] = $row1["Day"] $row1["Time"] $row1["TOD"]; } echo $array; Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/ Share on other sites More sharing options...
doni49 Posted April 4, 2008 Share Posted April 4, 2008 I'm not sure I understand exactly what you're trying to do but I see two different possibilities. I'll do what I can to answer both scenarios. If you want each element of your array to be one string of the information, you need to concatenate the strings together. while($row = mysql_fetch_array($result)){ $array[] = $row1["Day"] . " " . $row1["Time"] . " " . $row1["TOD"]; } If you want each element to be another array, then try this: while($row = mysql_fetch_array($result)){ $array[] = array($row1["Day"], $row1["Time"], $row1["TOD"]); } Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-508990 Share on other sites More sharing options...
sk121506 Posted April 4, 2008 Author Share Posted April 4, 2008 I changed a few things and added the 1st suggestion (which i am looking for). I added $array = array(); at the beginning to define an array and $row in the while statement to $row1, however, i am getting the result of just "Array" and not values from my db table. Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-508992 Share on other sites More sharing options...
doni49 Posted April 4, 2008 Share Posted April 4, 2008 Well what you just described sounds more like there's an error in your SQL code. NOT in the array code. You set the variable to array() and then when you run it thru print_r, it just shows array. That means that result is always blank and therefore the inside of the while loop never runs. Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-508996 Share on other sites More sharing options...
sk121506 Posted April 4, 2008 Author Share Posted April 4, 2008 I think its the way i'm defining $array[] inside the loop because i tried echo $row1["Day"] . " " . $row1["Time"] . " " . $row1["TOD"]; and that gave me the correct query result, just not getting put into the array. Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-509003 Share on other sites More sharing options...
sasa Posted April 4, 2008 Share Posted April 4, 2008 change echo $array; to print_r($array); Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-509017 Share on other sites More sharing options...
sk121506 Posted April 4, 2008 Author Share Posted April 4, 2008 well thats one step closer, except it printed the results out in an array format like Array ( [0] => 1 12:00 AM [1] => 1 12:03 AM....etc, any way of just printing the results? Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-509210 Share on other sites More sharing options...
wildteen88 Posted April 4, 2008 Share Posted April 4, 2008 Use $array[0]['Day'], $array[0]['Time'] and $array[0]['TOD'] to output the Day, Time and TOD for the first row returned from the query, $array[1]['Day'], $array[1]['Time'] and $array[1]['TOD'] for the secound row, $array[2]['Day'], $array[2]['Time'] and $array[2]['TOD'] for the third etc. You can simply loop through the $array variable using a foreach: foreach($array as $row_data) { list($day, $time, $tod) = array_values($row_data); echo '<p>Day: ' . $day . '<br >'; echo '<p>Time: ' . $time . '<br >'; echo '<p>TOD: ' . $tod . '</p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/#findComment-509386 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.