根据索引回显查询结果

发布于 2024-12-07 19:29:13 字数 499 浏览 1 评论 0原文

这是非常基本的,但我似乎无法让它工作

我有这个查询

$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 技术交流群。

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

发布评论

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

评论(3

童话 2024-12-14 19:29:13

您可以使用 WHERE 子句通过索引来获取它们。

$people = sprintf("SELECT name FROM people WHERE index='%d'", $index);

如果您想查询所有行,您可以将它们存储到一个数组中,同时循环它们:

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$totalRows_people = mysql_num_rows($people);
$rows_people = array();
while($row_people = mysql_fetch_assoc($people))
{
    $rows_people[] = $row_people;
}

您可能希望将主键添加到返回的字段中,并可能将其用作数组索引。

You can fetch them by their index using a WHERE clause.

$people = sprintf("SELECT name FROM people WHERE index='%d'", $index);

If you want to query all rows, you could store them into an array while looping over them:

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$totalRows_people = mysql_num_rows($people);
$rows_people = array();
while($row_people = mysql_fetch_assoc($people))
{
    $rows_people[] = $row_people;
}

You might want to add the primary key to the returned fields and use it as the array index probably.

指尖凝香 2024-12-14 19:29:13

您可以按索引对它们进行排序,然后使用循环。

$people = "SELECT name FROM people ORDER by index";

You can ORDER them by their index and then use a loop.

$people = "SELECT name FROM people ORDER by index";
巴黎盛开的樱花 2024-12-14 19:29:13

您可以在结果对象上使用 mysql_data_seek 来查找特定行。例如,要从第 2 行获取名称值:

mysql_data_seek($people, 2);
$row_people = mysql_fetch_assoc($people);
echo $row_people['name'];

如果您经常这样做,那么在开始时将所有行收集到一个数组中会更容易:

$data = array();
while ($row = mysql_fetch_assoc($people)) $data[] = $row;

这样您就可以轻松获取结果中的任何单元格:

echo $data[2]['name'];

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:

mysql_data_seek($people, 2);
$row_people = mysql_fetch_assoc($people);
echo $row_people['name'];

If you're doing this a lot it will be easier to gather all the rows together in a single array at the start:

$data = array();
while ($row = mysql_fetch_assoc($people)) $data[] = $row;

This way you can fetch any cells in the results trivially:

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