Andy17 Posted July 30, 2010 Share Posted July 30, 2010 Hey guys, I have a problem that I find rather strange, but maybe that's just me. I want to output a list of news that is stored in a MySQL database. I have coded paging ($start in the example below), but below I have narrowed my code down and removed code that is not related to the problem. The problem is that my script does not output the first row. That is, if I limit the query below from 0 to the next 10 (LIMIT 0, 10), the result will start at row 2. The same happens if $start is 10; news 12 will be the first one rather than 11. I have tried to put the numbers directly into the SQL query instead of $start, but with the same result. I have tested the query in phpMyAdmin, and the query works fine there. But for some reason, why PHP script does not output the first result. I guess I have made some silly mistake, but at the moment, I do not see it. <?php // Connect to MySQL & select db $start = 0; $result = mysql_query("SELECT * FROM Content ORDER BY Time ASC LIMIT $start, 10"); if ($result) { if (mysql_num_rows($result) > 0) { $row = mysql_fetch_assoc($result); while ($row = mysql_fetch_array($result)) { echo '<div class="someClass">' . $row['title'] . '</div>'; } } } ?> Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/ Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 $result = mysql_query("SELECT * FROM Content ORDER BY Time ASC LIMIT $start, 10"); As I see here you ordering by Time. Are you sure that your primary key is Time ? I don't think so, Try it to order by unique id from the table Content. mainly id ID or UID whatever. Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093055 Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Author Share Posted July 30, 2010 $result = mysql_query("SELECT * FROM Content ORDER BY Time ASC LIMIT $start, 10"); As I see here you ordering by Time. Are you sure that your primary key is Time ? I don't think so, Try it to order by unique id from the table Content. mainly id ID or UID whatever. Yeah, actually it is better to sort by the ID for a few reasons, but unfortunately it did not solve my problem. Thanks for the reply. Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093071 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 Even you can try like this $result = mysql_query("SELECT * FROM Content ORDER BY Time ASC, ID DESC LIMIT $start, 10"); // OR $result = mysql_query("SELECT * FROM Content ORDER BY Time ASC, ID ASC LIMIT $start, 10"); Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093072 Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Author Share Posted July 30, 2010 Still no luck. Thank you for trying, I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093074 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 I hope Time is a not UNIX time or timestamp. cheers! Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093076 Share on other sites More sharing options...
Wolphie Posted July 30, 2010 Share Posted July 30, 2010 Remove the line $row = mysql_fetch_assoc($result); and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093078 Share on other sites More sharing options...
inversesoft123 Posted July 30, 2010 Share Posted July 30, 2010 Remove the line $row = mysql_fetch_assoc($result); and it should work. Good eye Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093079 Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Author Share Posted July 30, 2010 I hope Time is a not UNIX time or timestamp. cheers! It's a datetime. I know it's not the best way to go about it, but this is a rather small project that will always use MySQL. Remove the line $row = mysql_fetch_assoc($result); and it should work. Oh God, I have no idea what that was doing there. Even worse, I cannot believe I didn't notice that it was wrong. Thanks for being more awake than me, lol. Quote Link to comment https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093085 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.