简体   繁体   中英

MySQL selecting Top 10 Records Based on an int field of a table

i have a table called Book that has a field(BorrowTime) that will increment once it's being borrowed. something like below

i want to retrieve the top 10 records with the highest borrowtime. my sql statement:

$results = mysql_query("SELECT * from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_array($results))
{
   echo $row['Title'];
}

it's not working and have infinite loop. does anyone know what goes wrong?

no problem with your code, but try debuging using this script,

$results = mysql_query("SELECT * from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_assoc($results))
{
    print_r($row);
    exit;//force exit script to debuging, are we get correct result or not
}

Change your code to:

$results = mysql_query("SELECT Title from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_array($results)){
   echo $row['Title'];
}

Because you are only using the field 'Title', and no other fields from that table.

Also, when this field is defined as 'title' than $row['Title'] wont show anything, because you should than use $row['title']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM