根据索引回显查询结果
这是非常基本的,但我似乎无法让它工作
我有这个查询
$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);
我可以回显这个查询的第一个结果
<?php echo $row_people['name']; ?>
我也可以创建一个循环并回显所有结果。
但我真的想根据其索引单独回显结果。
我已经尝试过这个,但它不起作用。
<?php echo $row_people['name'][2]; ?>
感谢您的帮助。
This is pretty basic but I can't seem to get it to work
I have this query
$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);
I can echo the first result of this query with
<?php echo $row_people['name']; ?>
I could also create a loop and echo all the results.
But I really want to echo the results individually based on its index.
I have tried this, but it does not work.
<?php echo $row_people['name'][2]; ?>
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 WHERE 子句通过索引来获取它们。
如果您想查询所有行,您可以将它们存储到一个数组中,同时循环它们:
您可能希望将主键添加到返回的字段中,并可能将其用作数组索引。
You can fetch them by their index using a WHERE clause.
If you want to query all rows, you could store them into an array while looping over them:
You might want to add the primary key to the returned fields and use it as the array index probably.
您可以按索引对它们进行排序,然后使用循环。
You can ORDER them by their index and then use a loop.
您可以在结果对象上使用 mysql_data_seek 来查找特定行。例如,要从第 2 行获取名称值:
如果您经常这样做,那么在开始时将所有行收集到一个数组中会更容易:
这样您就可以轻松获取结果中的任何单元格:
You can use mysql_data_seek on the result object to seek to a particular row. E.g., to get the name value from row 2:
If you're doing this a lot it will be easier to gather all the rows together in a single array at the start:
This way you can fetch any cells in the results trivially: