2011-04-25 142 views
0

我有一个名为Book的表,它有一个字段(借位时间),一旦它被借用就会增加。像下面的东西MySQL选择前10条记录基于表中的int字段

我想检索具有最高借用时间的前10条记录。 我的sql语句:

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

它不工作,并有无限循环。有谁知道哪里出了问题?

+0

你确定这是代码经历一个无限循环 – JohnP 2011-04-25 07:19:37

+0

代码是正确的。问题在别的地方。 – Emmerman 2011-04-25 07:29:44

+0

似乎,很好..尝试更改变量名称,以避免与其他循环冲突 – ariel 2011-04-25 07:29:46

回答

0

与您的代码没有问题,但尝试使用这个脚本调试运行,

$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 
} 
-1

更改您的代码:

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

因为你只使用领域“标题”,并没有其他的来自该表的字段。

此外,在该领域被定义为“标题”超过$行[“标题”]不会显示任何东西,因为你应该比使用$行[“标题”]