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

警告: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 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?