icor1031 Posted September 17, 2014 Share Posted September 17, 2014 $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 More sharing options...
trq Posted September 17, 2014 Share Posted September 17, 2014 TOP is not valid syntax in MySQL. Link to comment https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491368 Share on other sites More sharing options...
icor1031 Posted September 17, 2014 Author Share Posted September 17, 2014 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 More sharing options...
mac_gyver Posted September 17, 2014 Share Posted September 17, 2014 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 More sharing options...
icor1031 Posted September 17, 2014 Author Share Posted September 17, 2014 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: Here, you see that my PID began (before my "fix") one number higher than what exists in the database: Column stuff: Link to comment https://forums.phpfreaks.com/topic/291118-php-sql-top/#findComment-1491382 Share on other sites More sharing options...
Barand Posted September 17, 2014 Share Posted September 17, 2014 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 More sharing options...
icor1031 Posted September 17, 2014 Author Share Posted September 17, 2014 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.