php打印数组的10个结果

发布于 2024-10-17 10:33:42 字数 522 浏览 4 评论 0原文

我有个问题想问你们。 我正在尝试打印一个数组,其中它将根据用户显示表中的 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 行? 我们将不胜感激。 感谢您的阅读。

i have a question for you guys.
im trying to print an array where it would display 10 values of the table acording to user.
this is what i have so far and it displays only the first row,

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'];

.....
how can i display the first 10 rows?
help would be apreciated.
thank you for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲸落 2024-10-24 10:33:42

还将“limit 10”添加到查询中。

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

also add "limit 10" to the query.

SELECT name,location,login_id FROM table WHERE login_id = $user[login_id] LIMIT 10
鹤舞 2024-10-24 10:33:42
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时,这才有效

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'];
}

And This will work only if your login_id is not unique and multiple rows can have the same login_id

放低过去 2024-10-24 10:33:42

您必须重复调用 mysql_fetch_array 才能从结果集中获取所有行:

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

请参阅文档中的更多示例

如果您确实只想获取前 10 行,请查看 @Yasser Souri 的回答(不过你仍然需要循环)。

You have to call mysql_fetch_array repeatedly to get all the row from the result set:

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

See further examples in the documentation.

If you really want to get only the first 10 rows, have a look at @Yasser Souri's answer (you still have to loop though).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文