在PHP中调用while循环参数内的函数
我已经被这个概念困扰了5个小时了,这真的让我很沮丧在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?好的,这里有一个简单
$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!!你能解释一下答案出了什么问题吗?这个令人沮丧的选民!!你能解释一下这个答案有什么问题吗?