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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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