doulai2025 2017-11-10 10:36
浏览 251
已采纳

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

I have this warning but I have not found what causes this error, please guys can you light me? The error is in line 58 ---> $data = mysqli_fetch_assoc($result) or die();{ I searched for a while but can't find how i tacle that error called.

"Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:.... on line 58"

<?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条回答 默认 最新

  • dsds661730652211 2017-11-10 12:00
    关注

    The function mysqli_query() returns FALSE on error. If you pass this return value to mysqli_fetch_assoc() without checking it first, you get the warning/error you describe. You should therefore check the return value of mysqli_query(), and if it is FALSE, you can get the error from mysqli_error(), e.g.:

    $result = mysqli_query($connect, $sql);
    if ($result === FALSE) {
      die(mysqli_error($connect));
    }
    $data = mysqli_fetch_assoc($result);
    return $data;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印