Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am getting time from my database, when I echo it out it works, but it doesn't work in the while loop. However, the other variables work...

Code:

$resultevent = mysql_query('SELECT  venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');
$rowevent = mysql_fetch_assoc($resultevent);

$date = new DateTime($originaldate);
$newdate = $date->format('m/d/Y');
$startTime2 = $rowevent['startTime'];
$endTime2 = $rowevent['endTime'];
$venue = $rowevent['venue'];

echo $startTime2; <============== WORKS HERE

while ($row = mysql_fetch_assoc($result)) { //the code for these queries is not shown
   echo 'getting here'; <======= WORKS
   echo $venue; <======= WORKS
   echo $startTime2; <======= DOESNT WORK
}
share|improve this question
 
there's no variable called $result for starters? –  Breezer Oct 8 at 2:34
 
Are you sure this code is exactly as you have it? And that the lines you say "work" are indeed where you think they are? I would recommend modifying your echo statements to look more like this: "At line 10, we echo $startTime2 as " . $startTime2; - this makes it unambiguous which lines are doing what. –  Floris Oct 8 at 2:35
 
change echo $startTime2; to echo "-".$startTime2."-"; so you can make sure that the start time variable isn't being printed out. Maybe it's null. If it is null then "--" will be printed out. –  Rawr Oct 8 at 2:37
 
in the comment i wrote that i didn't show some of the code that deals with the while statement –  nshah Oct 8 at 3:03
 
it is null, but idk how, before the while loop it is not null –  nshah Oct 8 at 3:04

2 Answers

up vote 1 down vote accepted

Your query is returning your time columns with an alias. To read them from your row you should use the alias as an index:

$startTime2 = $rowevent['startTime2'];
$endTime2 = $rowevent['endTime2'];
share|improve this answer
 
Good observation. Why do you think that echo $startTime2; works outside of the while loop though? That's what was bothering me... –  Floris Oct 8 at 2:36
 
I don't know. Your code is incomplete, so maybe it's left over from something else. –  Mike W Oct 8 at 2:37
 
Yeah, idk why it works from outside the while loop. Nevertheless, thank you! –  nshah Oct 8 at 3:06

1.In your loop where does $results came from?? 2.You set startTime as startTime2 in your query same as endTime to endTime2. and obviously it will not work even your echo $venue.. because you fetch nothing.

it should be like this code:

    $date = new DateTime($originaldate);
    $resultevent = mysql_query('SELECT  venue, date, TIME_FORMAT(startTime, "%h:%i %p") AS startTime2, TIME_FORMAT(endTime, "%h:%i %p") AS endTime2 FROM events"');

    while ($rowevent = mysql_fetch_assoc($resultevent)) { //now you fetch your query in $resultevent

       $newdate = $date->format('m/d/Y');
       $startTime2 = $rowevent['startTime2'];
       $endTime2 = $rowevent['endTime2'];
       $venue = $rowevent['venue'];

       echo 'getting here';
       echo $venue; 
       echo $startTime2;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.