mkultron Posted February 7, 2012 Share Posted February 7, 2012 Hi guys, I'm having a bit of bother running this while loop, it's infinite. Could you give me some tips on getting it fixed please? while($row = mysql_fetch_array( $result )) { $stuffandthings = mysql_result($result, $i, 'stuffandthings'); echo '<td>' . $row['stuffandthings'] . '</td>'; } Thanks MK Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/ Share on other sites More sharing options...
scootstah Posted February 7, 2012 Share Posted February 7, 2012 You don't need the mysql_result in there. Just do while($row = mysql_fetch_array( $result )) { echo '<td>' . $row['stuffandthings'] . '</td>'; } Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1315507 Share on other sites More sharing options...
mkultron Posted February 8, 2012 Author Share Posted February 8, 2012 Sorry I actually need it set out like this (with the line you replaced) as it's the only way I seem to be able to format the date the way I want it without it setting everythig to today's date, however when I use the line you took out it produces an infinite loop. Any idea how I can avoid this while still retaining the correct formatted date? while($row = mysql_fetch_array( $result )) { $mydate = mysql_result($result, $i, 'mydate'); $dt = new DateTime($mydate); echo '<td>' . $dt->format("jS F Y, g:i a") . '</td>'; } Thanks MK Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1315712 Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2012 Share Posted February 8, 2012 You don't need to use mysql_result. mysql_fetch_array already places each column value into the $row array. You just need to reference the $row['column_name_here'] that holds your date. Also, why don't you simply select the date formated the way you want it in your query? There's no need for any slow parsed, tokenized, interpreted php code. Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1315715 Share on other sites More sharing options...
mkultron Posted February 8, 2012 Author Share Posted February 8, 2012 Oh OK, could you tell me how I'd go about formatting the SELECT statement? $result = mysql_query("SELECT * FROM stuff.test ORDER BY mydate ASC") I take it I can put a "WHERE" following ASC and then format "jS F Y, g:i a". Many thanks MK Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1315739 Share on other sites More sharing options...
scootstah Posted February 8, 2012 Share Posted February 8, 2012 Assuming that your date column is called "date" and is of type DATETIME SELECT DATE_FORMAT(date, %D %M %Y, %l:%i %p) AS date ... Then you can reference it like while($row = mysql_fetch_assoc($result)) { echo $row['date']; } Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1315818 Share on other sites More sharing options...
mkultron Posted February 9, 2012 Author Share Posted February 9, 2012 OK, thanks. I tried this and it posts the first result once and the second one over and over (btw I'm pulling more values out of the database using * but I've left them out for the purpose of this post). $result = mysql_query("SELECT *, DATE_FORMAT(mydate, '%a %b %e %Y %H:%i') AS mydate FROM stuff.eventstest ORDER BY mydate ASC") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['mydate']; } Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1316145 Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 I'm not sure I understand. Can you copy & paste the output? Link to comment https://forums.phpfreaks.com/topic/256614-inifite-while-loop-terror/#findComment-1316157 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.