错误:警告:mysqli_fetch_array()期望参数1为mysqli_result

时间:2012-01-03 16:56:27

标签: php

  

可能重复:
  Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error

我知道这已被解决了一百万次,但我似乎无法弄明白。我不断收到此错误: 警告:mysqli_fetch_array()要求参数1为mysqli_result,第24行中给出布尔值 。到目前为止,这是我的代码:

<?php

//Connect to the database
require('mysql_connect.php') ;

$query = "SELECT * FROM students WHERE alumni = 'Yes' AND shipped = 'Yes'" ;
$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc) . '<br />' . $query) ;
$num = mysqli_num_rows($result) ;


if ($num >= 1) {

    while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {

    $full_name = $row['first_name'] . ' ' . $row['middle_name'] . ' ' . $row['last_name'] ;

    $query = "INSERT INTO alumni_center (student_id, full_name, phone, address, city, state, postal_code, enrollment_url, active, enrollment_date) VALUES ({$row['student_id']}, '$full_name', '{$row['phone']}', '{$row['address']}', '{$row['city']}', '{$row['state']}', '{$row['postal_code']}', '{$row['registration_url']}', 'Yes', '{$row['alumni_registration_date']}')" ;
    $result = mysqli_query($dbc, $query) or die(mysqli_error($dbc) . '<br />' . $query) ;

    }

} else {

    echo 'Only 1' ;
}

echo 'Done' ;

?>

我在phpmyadmin中运行了两个查询,并且查询工作正常。而不是通过所有学生循环,它只是通过1学生,并将它们插入alumni_center表。它应该做所有学生,而不仅仅是1.非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您正在使用INSERT语句返回的$ result值覆盖SELECT查询返回的$ result值,因此第二次通过循环mysqli_fetch_array($ result,MYSQLI_BOTH)尝试从结果中检索一行你的INSERT,而不是SELECT的下一个结果

PS

为什么不简单地使用

INSERT INTO alumni_center 
 SELECT ... FROM students ...