Jump to content

PHP - SQL: TOP


Go to solution Solved by Barand,

Recommended Posts

    $results = mysqli_query($con, 'SELECT TOP 8 * FROM Entries');
    var_dump($results);

This gets me bool(false)

 

------------

    $results = mysqli_query($con, "SELECT * FROM Entries ORDER BY PID DESC LIMIT 9");

But, this gets me the proper results - it draws from my database, like it should.

 

Why do I get false, when I use TOP 8?

 

 

Thanks.

Edited by icor1031
Link to comment
https://forums.phpfreaks.com/topic/291118-php-sql-top/
Share on other sites

Woops, I misunderstood this: http://www.w3schools.com/sql/sql_top.asp

 

 

I've been using limit, but the problem is that it doesn't call the most recent entry - even if I use LIMIT 0,8.

And LIMIT -1,8 gives an error.

 

Should I find the PID, then +1 it?

 

Any advice?

 

 

Thanks!

Edited by icor1031
Link to comment
https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491370
Share on other sites

you should probably post an example of the incorrect data the query is matching and an example of the data the query should match, along with the table definition. if your order by isn't returning the correct value, it's likely that your table definition is storing numbers as a character/text type.

Link to comment
https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491372
Share on other sites

I "fixed" it, by adding the third line. I'm not satisfied with that result, but it works. For some reason, the while statement started one item too late.

	$results = mysqli_query($con, "SELECT * FROM Entries ORDER BY PID DESC LIMIT 0,8");
	$rowFp = mysqli_fetch_array($results);
	echo "<br /><br />" . $rowFp['Title'] . "<br />" . $rowFp['remoteTime'] . "<br /><br />" . $rowFp['PID'];
	while  ($rowFp = mysqli_fetch_array($results)) {
	echo "<br /><br />" . $rowFp['Title'] . "<br />" . $rowFp['remoteTime'] . "<br /><br />" . $rowFp['PID'];

Here, you see my last PID:

 

Ef8mKIq.png

 

 

Here, you see that my PID began (before my "fix") one number higher than what exists in the database:

 

JqzTK7w.png

 

 

 

Column stuff:

7Iw9ur6.png

Edited by icor1031
Link to comment
https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491382
Share on other sites

  • Solution

Delete lines 2 and 3. Just fetch and output with the while() loop.

$results = mysqli_query($con, "SELECT * FROM Entries ORDER BY PID DESC LIMIT 0,8");
	
while  ($rowFp = mysqli_fetch_array($results)) {
	echo "<br /><br />" . $rowFp['Title'] . "<br />" . $rowFp['remoteTime'] . "<br /><br />" . $rowFp['PID'];
}
Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491383
Share on other sites

 

Delete lines 2 and 3. Just fetch and output with the while() loop.

$results = mysqli_query($con, "SELECT * FROM Entries ORDER BY PID DESC LIMIT 0,8");
	
while  ($rowFp = mysqli_fetch_array($results)) {
	echo "<br /><br />" . $rowFp['Title'] . "<br />" . $rowFp['remoteTime'] . "<br /><br />" . $rowFp['PID'];
}

 

Awesome, thanks.

Link to comment
https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491384
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.