Jump to content

array of retrieving multiple rows??


sk121506

Recommended Posts

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;

Link to comment
https://forums.phpfreaks.com/topic/99482-array-of-retrieving-multiple-rows/
Share on other sites

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"]);
}

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.

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.

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>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.