Jump to content

PHP - SQL: TOP


icor1031

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.

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!

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

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

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.