Jump to content

Wrong query output


Andy17

Recommended Posts

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.  :sleeping:

 

 

<?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!

Link to comment
https://forums.phpfreaks.com/topic/209336-wrong-query-output/
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093055
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093071
Share on other sites

I hope Time is a not UNIX time or timestamp.

 

;D 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. :)

Link to comment
https://forums.phpfreaks.com/topic/209336-wrong-query-output/#findComment-1093085
Share on other sites

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.