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