我有一个名为Book的表,它有一个字段(BorrowTime),一旦被借用,它将增加。像下面的
我想检索前10名记录与最高的借款时间。我的SQL语句
$results = mysql_query("SELECT * from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_array($results))
{
echo $row['Title'];
}
不工作,有无限循环。有人知道哪里出了问题吗?
您的代码没有问题,但请尝试使用此脚本进行调试,
$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
}
修改代码为:
$results = mysql_query("SELECT Title from books order by BorrowTime DESC LIMIT 10");
while ($row = mysql_fetch_array($results)){
echo $row['Title'];
}
因为您只使用了字段'Title',而没有使用该表中的其他字段。
同样,当这个字段被定义为'title'时,$row['title']不会显示任何东西,因为你应该使用$row['title']