2011-02-14 87 views
0

我有一个问题给你们。 即时通讯设法打印一个阵列,它将显示根据用户表的10个值。 这是我有这么远,只显示第一行,php打印阵列的10个结果

session_start(); 
// Retrieve all the data from the table 
$result = mysql_query("SELECT name,location,login_id FROM table WHERE login_id = $user[login_id]") 
or die(mysql_error()); 

// store the record of the "example" table into $row 
$row = mysql_fetch_array($result); 
// Print out the contents of the entry 

echo " name ".$row['name']; 
echo " located ".$row['location']; 

..... 我怎样才能显示前10行? 帮助将被折衷。 感谢您的阅读。

+0

其实,你的数组只包含**一个**结果 – 2011-02-14 10:55:30

+0

@Col。弹片:你怎么知道的? – 2011-02-14 10:56:46

+0

魔法!通过阅读代码 – 2011-02-14 10:58:32

回答

1
session_start(); 
// Retrieve all the data from the table 
$result = mysql_query("SELECT name,location,login_id FROM table WHERE login_id = $user[login_id] LIMIT 10") 
or die(mysql_error()); 

while($row = mysql_fetch_array($result)){ 
    echo " name ".$row['name']; 
    echo " located ".$row['location']; 
} 

如果你的LOGIN_ID是不是唯一的,多行可以有相同的LOGIN_ID

0

你必须调用mysql_fetch_array反复从结果集中获取所有行:

while(($row = mysql_fetch_array($result))) { 
    echo " name ".$row['name']; 
    echo " located ".$row['location']; 
} 

See further examples in the documentation

如果你真的只想得到前10行,看看@Yasser Souri's answer(你仍然需要循环)。

4

还加上“限10”到查询这只会工作。

SELECT name,location,login_id FROM table WHERE login_id = $user[login_id] LIMIT 10