警告:mysqli_fetch_assoc()期望参数1为mysqli_result,

时间:2017-11-10 10:36:29

标签: php

我有这个警告但是我没有找到导致这个错误的原因,请你们点亮我吗? 错误在第58行---> $ data = mysqli_fetch_assoc($ result)或die(); { 我搜索了一会儿,但无法找到我所说的错误。

"警告:mysqli_fetch_assoc()要求参数1为mysqli_result,在第58行和第34行的C:....中给出布尔值;

<?php
require_once('connect.php');

$user_id = 0;

function print_header($title="Header") { ?>

<title>PDS</title>


<html>
<body>

<?php 


global $user_id;
if (logged_in()){
    $session_user_id = $_COOKIE['pds-usn'];
    $user_data = user_data($session_user_id, 'id', 'name', 'hashed_pw', 'email', 'last_name', 'email', 'site', 'level', 'joined', 'posts', 'type');
    echo $user_data['name'];
    echo 'U bent ingelogd als '.$_COOKIE['pds-usn'].'. <a href="login.php?logout=1">Uitloggen</a>';
}   else {
    echo "U bent niet ingelogd. <a href='login.php'>Login</a> of <a href='register.php'>registreer</a>.";
}



?>


<?php }


function print_footer() { ?>

</body>
</html>

<?php }

function user_data($user_id) {

include('connect.php');

$data = array();
$user_id = (int)$user_id;

$func_num_args = func_num_args();
$func_get_args = func_get_args();

if ($func_num_args > 0) {
    unset($func_get_args[0]);

    $fields = '`' . implode('`, `', $func_get_args) . '`';
    $sql = "SELECT $fields FROM users WHERE id = $user_id";
    $result = mysqli_query($connect, $sql);
    $data = mysqli_fetch_assoc($result) or die();
    return $data;

}   
}else {
    return false;
}

?>

Connect.php:

<?php

$connect = mysqli_connect("localhost", "root", "password") or die(header('Location: errorpage.php?error=2'));
mysqli_select_db($connect, "pds_news_feed") or die(header('Location: errorpage.php?error=3'));
?>

1 个答案:

答案 0 :(得分:0)

函数mysqli_query()在出错时返回FALSE。如果将此返回值传递给mysqli_fetch_assoc()而不先检查它,则会收到您描述的警告/错误。因此,您应该检查mysqli_query()的返回值,如果是FALSE,则可以从mysqli_error()获取错误,例如:

$result = mysqli_query($connect, $sql);
if ($result === FALSE) {
  die(mysqli_error($connect));
}
$data = mysqli_fetch_assoc($result);
return $data;