Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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_Mysql - Fatal编程技术网

若数据库并没有结果,PHP while语句会给我错误

若数据库并没有结果,PHP while语句会给我错误,php,mysql,Php,Mysql,若数据库并没有结果,PHP while语句会给我错误。如果它在数据库中什么都看不到,为什么它就不能执行呢 while ($row = mysqli_fetch_assoc($result_tomorrow)) { $rowsB[] = $row['last']; } 我得到的错误是未定义的变量$rowsB。当我把一些东西放进数据库时,一切正常。我还尝试将while循环包装到if语句中 if(results){ while ($row = mysqli_fetch_ass

若数据库并没有结果,PHP while语句会给我错误。如果它在数据库中什么都看不到,为什么它就不能执行呢

while ($row = mysqli_fetch_assoc($result_tomorrow)) {
       $rowsB[] = $row['last'];
}
我得到的错误是未定义的变量$rowsB。当我把一些东西放进数据库时,一切正常。我还尝试将while循环包装到if语句中

if(results){
     while ($row = mysqli_fetch_assoc($result_tomorrow)) {
            $rowsB[] = $row['last'];
            }
}

这也不起作用

是错误还是通知?您应该在while循环之前定义变量。

试试这个

if(mysqli_num_rows($result_tomorrow) > 0){
    while ($row = mysqli_fetch_assoc($result_tomorrow)) {
        $rowsB[] = $row['last'];
    }
}

首先检查数据库是否返回了一些内容,然后执行while语句

$result = mysqli_query($connection, $query); //execute the query

if( $result ){ //on successful query execution
 if( mysqli_num_rows($result) > 0 ){ //check for number of returned rows. If its > 0
   while( $row = mysqli_fetch_assoc($result) ){ //execute for loop
    //other statements
   }
 }
}

如果只有当查询没有返回结果时才出现错误,那么这是因为您试图在稍后使用$rowsB而不声明它。请注意错误是针对哪一行的。您应该始终检查查询结果是否也有效:

$rowsB = array();
if ($result_tomorrow) {
    while ($row = mysqli_fetch_assoc($result_tomorrow)) {
       $rowsB[] = $row['last'];
    }
}

美元结果从何而来?这是什么?当然这是一个通知,而不是一个错误。你应该声明你的变量,只说它是一个数组,或者只是用mysqli_num_rows函数检查你的查询结果,始终将所有错误消息全部复制并粘贴到问题中声明变量为数组是正确的通知将不再显示。首先从while循环外部声明变量是正确的。这是我需要做的事情。。但是if语句不起作用。如果结果集中有行,为什么while循环的工作方式会有所不同?