MySQL selecting Top 10 Records Based on an int field of a table
i have a ta开发者_JAVA百科ble 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']
精彩评论