0

I'm trying to select the top 5 results, but whenever adding order by votes desc limit 5 to this code:

$req2 = mysql_query('select id, url, name, description, banner, votes 
                     from topsite 
                     order by votes desc 
                     limit '.$first_message .','.$last_message); 
while($dnn2 = mysql_fetch_array($req2))

it won't work. How can I fix it?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Mattxd
  • 9
  • 2
  • 2
    Please dont use the `mysql_` database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Nov 11 '15 at 16:06
  • What are the $first_message and $last_message? To get only the top 5, just order by descending order and limit to 5. – Ron Dadon Nov 11 '15 at 16:07
  • *"it won't work"* please be more specific – m02ph3u5 Nov 11 '15 at 16:16
  • @m02ph3u5, Whenever I refresh the page after adding `order by votes desc limit 5` it will just show a blank page. No errors. – Mattxd Nov 11 '15 at 16:19
  • Maybe you got the *LIMIT* wrong. It's `LIMIT , `. Look at this example from the docs: `SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15` https://dev.mysql.com/doc/refman/5.6/en/select.html - `$last_message` is most probably not what it should be – m02ph3u5 Nov 11 '15 at 16:22
  • @m02ph3u5 Yes it wrong. He only actually need `LIMIT 5` as all he wants is the TOP5 – RiggsFolly Nov 11 '15 at 16:29

3 Answers3

1

If you only want the TOP 5 then you only need one limiter like this

Also if you use double quotes around your query you can expand $variables automatically, which makes it a lot easier to read and debug.

Also if you want to see the errors produced by the mysql_ extension you have to look for them

$top_5_please = 5;

$req2 = mysql_query("select id, url, name, description, banner, votes 
                     from topsite 
                     order by votes desc 
                     limit $top_5_please"); 

// error check
if ( ! $req2 ) {
    echo mysql_error();
    exit;
}

while($dnn2 = mysql_fetch_array($req2))

FINAL NOTE

Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

You can try the following. Also note that mysql_* functions are deprecated. Use PDO instead.

$req2 = mysql_query('select id, url, name, description, banner, votes 
                     from topsite 
                     order by votes desc 
                     limit '.$your_limit .' OFFSET '.$your_offset); 
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37
0
$req2 = mysql_query('select id, url, name, description, banner, votes 
                     from topsite 
                     order by votes desc 
                     limit 5');

you can't pass string into limit you have to use integer only in limit

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149