mysql_fetch_row 不返回完整数组

发布于 2024-12-18 05:50:19 字数 529 浏览 0 评论 0原文

我正在尝试使用以下代码循环遍历 MySQL 中的行。不幸的是, mysql_fetch_row 函数仅返回查询的第一行(我用 print_r 确认了这一点)。当我使用 mysql_num_rows 时,它会返回该查询的正确行数(​​2)。知道为什么 mysql_fetch_row 只返回第一行吗?

$query = 'SELECT firstName FROM profiles WHERE city="Phoenix" and lastName ="Smith"';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
print_r($row); //This returns only 1 array item: Array ( [0] => John)

$a=mysql_num_rows($result);
print_r($a); //This returns 2

我还在 MySQL 中运行了相同的查询,它返回两行(“John”和“Jim”)。

感谢您的帮助。

I am attempting to loop through rows in MySQL with the following code. Unfortunately, the mysql_fetch_row function is only returning the first row of my query (I confirmed this with print_r). When I use mysql_num_rowsit returns the correct number of rows for that query(2). Any idea why mysql_fetch_row is returning only the first row?

$query = 'SELECT firstName FROM profiles WHERE city="Phoenix" and lastName ="Smith"';
$result = mysql_query($query);
$row = mysql_fetch_row($result);
print_r($row); //This returns only 1 array item: Array ( [0] => John)

$a=mysql_num_rows($result);
print_r($a); //This returns 2

I have also run the same query in MySQL and it returns both rows ("John" and "Jim").

Thanks for the help.

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

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

发布评论

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

评论(4

写给空气的情书 2024-12-25 05:50:19

只需执行 mysql_fetch_row 两次(或循环执行)。根据定义(如果您阅读文档)它一次仅返回一行。所以:

while ($row = mysql_fetch_row($result)) {
    var_dump($row);
}

Just perform mysql_fetch_row twice (or in a loop). By definition (if you read the documentation) it returns only one row at once. So:

while ($row = mysql_fetch_row($result)) {
    var_dump($row);
}
锦爱 2024-12-25 05:50:19

您想将其放入 while循环:

while ($row = mysql_fetch_array($result)) {

   $firstName = $row['firstName'];

   echo("$firstName<br />");

}

它的作用是对于它找到的每个结果(行),它获取您想要的数据并显示它。因此它获取第一个结果,显示名称,循环回到顶部,获取第二个结果,显示该名称等等,然后一旦没有更多与查询条件匹配的结果,while 循环就会结束。

You want to put it in a while loop:

while ($row = mysql_fetch_array($result)) {

   $firstName = $row['firstName'];

   echo("$firstName<br />");

}

What this does is for each result (row) it finds, it fetches the data you want and displays it. So it gets the first result, displays the name, loops back to the top, fetches the second result, displays that name and so on, then once you have no more results that match the query criteria, the while loop is ended.

撩动你心 2024-12-25 05:50:19

PHP 按照 SQL 结果的缓冲顺序来访问它们。您需要运行一个循环才能访问所有内容。

如果您希望将所有内容加载到数组中:

$allRows=array();
while($row=mysql_fetch_assoc($result) {
  $allRows[]=$row;
}

PHP accesses the SQL Results in order of how they're buffered. You'll need to run a loop to access all the contents.

If you wish to load everything into an array:

$allRows=array();
while($row=mysql_fetch_assoc($result) {
  $allRows[]=$row;
}
剪不断理还乱 2024-12-25 05:50:19

您可以使用以下任意一项:

while ($row = mysql_fetch_array($result)) 
{
   echo $row['firstName'];
}

while ($row = mysql_fetch_assoc($result)) 
{
   echo $row['firstName'];
}

while ($row = mysql_fetch_object($result)) 
{
    echo $row->firstName;
}

You can use any of the following one:

while ($row = mysql_fetch_array($result)) 
{
   echo $row['firstName'];
}

or

while ($row = mysql_fetch_assoc($result)) 
{
   echo $row['firstName'];
}

or

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