为什么mysqli_fetch_assoc会生成错误

时间:2016-06-11 09:12:09

标签: php mysqli

这个问题已经被提出,我已经按照了答案但我仍然得到错误' mysqli_fetch_assoc()期望参数1是mysqli_result'

我的代码

$host = "localhost";
$user = "my_user";
$user_pass = "my_password";
$dbase = "my_database";

$conn = mysqli_connect($host, $user, $user_pass, $dbase );
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql="select id, customer_name, date_ordered, amount from orders where status_id=5 and date_ordered >= '2016-01-01 00:00:00'";
$result = mysqli_query($conn, $sql);

if($result === FALSE) { 
    die(mysqli_error($mysqli));
}

var_dump($result);

echo 'test =  '.mysqli_num_rows($result).'<br />';
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
      //rest of code goes here
    }
}

var_dump行返回:

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(329) ["type"]=> int(0) } 

while($ row = mysqli_fetch_assoc($ result))行返回

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result

我真的不知道自己做错了什么。

由于

1 个答案:

答案 0 :(得分:0)

傻傻的我!

我在while循环中重置变量$ result。我做了调整,现在代码正在运行