Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中调用while循环参数内的函数_Php_Arrays_Mysqli_While Loop - Fatal编程技术网

在PHP中调用while循环参数内的函数

在PHP中调用while循环参数内的函数,php,arrays,mysqli,while-loop,Php,Arrays,Mysqli,While Loop,我已经被这个概念困扰了5个小时了,这真的让我很沮丧 $result = $mysqli->query("SELECT col FROM table"); while($row = $result->fetch_assoc()){ echo $row['data']; } 我知道这个函数一次只获取一行,但我不知道当循环重置时它是如何被调用的。在$row=$result->fetch_assoc中如何调用它?另外,如果$row为空,该条件如何计算为true?好的,这里有一个简单

我已经被这个概念困扰了5个小时了,这真的让我很沮丧

$result = $mysqli->query("SELECT col FROM table");
while($row = $result->fetch_assoc()){
    echo $row['data'];
}

我知道这个函数一次只获取一行,但我不知道当循环重置时它是如何被调用的。在
$row=$result->fetch_assoc
中如何调用它?另外,如果
$row
为空,该条件如何计算为true?

好的,这里有一个简单的测试

设一个
数组
,其值为
null
,如下所示

$row = array('value' => null);
现在,让我们使用
if
条件来检查这一点

if($row)
{
    echo "null is making variable value to true so condition will work.";
}
粘贴
code
run
我相信您会看到
if
条件中的消息

您的状况

您正在使用
$result->fetch_assoc()
,因此您知道它将返回
数组
,该数组可能具有
null
值,如上面的示例所示

但正如您所看到的,它将为
$result
返回
true
,因为
$result
实际上已赋值,并且它是
true


因此条件将得到满足。

好的,这里有一个简单的测试

设一个
数组
,其值为
null
,如下所示

$row = array('value' => null);
现在,让我们使用
if
条件来检查这一点

if($row)
{
    echo "null is making variable value to true so condition will work.";
}
粘贴
code
run
我相信您会看到
if
条件中的消息

您的状况

您正在使用
$result->fetch_assoc()
,因此您知道它将返回
数组
,该数组可能具有
null
值,如上面的示例所示

但正如您所看到的,它将为
$result
返回
true
,因为
$result
实际上已赋值,并且它是
true


因此条件将得到满足。

简而言之,while循环条件正在寻找
true

while(true){
//do this
}
因此,在表达式
$row=$result->fetch_assoc()
解析为
true
之前,while循环将继续


问题是什么时候它会不正确?检索所有行时。

简而言之,while循环条件正在查找
true

while(true){
//do this
}
因此,在表达式
$row=$result->fetch_assoc()
解析为
true
之前,while循环将继续


问题是什么时候它会不正确?检索所有行时。

以下代码解释可能会对您有所帮助

// run the query. this returns a mysqli_result object on success.
// on failure query function returns false and you have to handle this condition.
$result = $mysqli->query("SELECT col FROM table");

// fetch_assoc() here you fetch one row at a time from the mysqli_result
// object into $row. this also moves the pointer to the next row
// so the next call returns the next row.
// This returns false when there is no more rows and exit your while loop
while($row = $result->fetch_assoc()){
    echo $row['data'];
}
您可能希望参考的链接


以下代码解释可能会对您有所帮助

// run the query. this returns a mysqli_result object on success.
// on failure query function returns false and you have to handle this condition.
$result = $mysqli->query("SELECT col FROM table");

// fetch_assoc() here you fetch one row at a time from the mysqli_result
// object into $row. this also moves the pointer to the next row
// so the next call returns the next row.
// This returns false when there is no more rows and exit your while loop
while($row = $result->fetch_assoc()){
    echo $row['data'];
}
您可能希望参考的链接

如果$row为null,该条件如何计算为true

没有。这就是重点

如果$row为null,该条件如何计算为true

没有。这就是重点。

给定您的代码:

while($row = $result->fetch_assoc()){
    echo $row['data'];
}
循环条件可以重写为以下等效条件:

while (($row = $result->fetch_assoc()) != false) {
    echo $row['data'];
}
换句话说,虽然
$row
不是falsy,但它将继续循环
null也被认为是虚假的。

给定您的代码:

while($row = $result->fetch_assoc()){
    echo $row['data'];
}
循环条件可以重写为以下等效条件:

while (($row = $result->fetch_assoc()) != false) {
    echo $row['data'];
}

换句话说,虽然
$row
不是falsy,但它将继续循环<代码>空值
也被认为是错误的。

你所说的“当循环重置时”是什么意思?
while
循环不会重置。您所说的“如果
$row
为空,该条件如何计算为真”是什么意思?因为如果
$row
NULL
它的计算结果不为
true
,所以您可能应该定义您所询问的条件。另外,我想知道您查看了PHP手册的哪些部分,以便更好地理解此问题。应该对此进行解释,如果在导航时遇到问题,您可能需要一些帮助。您必须打印($row)以查看其结构我意识到$row变量是在while语句中设置的,因此$row实际上不是null“当循环重置时”是什么意思?
while
循环不会重置。您所说的“如果
$row
为空,该条件如何计算为真”是什么意思?因为如果
$row
NULL
它的计算结果不为
true
,所以您可能应该定义您所询问的条件。另外,我想知道您查看了PHP手册的哪些部分,以便更好地理解此问题。应该对此进行解释,如果您在导航时遇到问题,您可能需要一些帮助。您必须打印($row)以查看其结构我意识到$row变量是在while语句中设置的,因此$row实际上不是空的。这就是它的真正含义,很简单,很简单downvoter!!你能解释一下答案出了什么问题吗?这个令人沮丧的选民!!你能解释一下这个答案有什么问题吗?