【问题标题】:Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result
【发布时间】:2022-12-31 15:01:04
【问题描述】:

您好我是 php 和 sql 的新手。我使用 php 我的管理员作为我的数据库并尝试编写我的第一个代码。

<?php
    $servername = "localhost";
    $username = "id20039740_antmx";
    $password = "\hlA|{Px]]SZJb4^";
    
    $conn = new mysqli($servername, $username, $password);
    $sql = "select * from mst_ID";
    $result = mysqli_query($conn, $sql);
    
    while($row  = mysqli_fetch_assoc($result)) {
        echo $row["id"] . $row["status"];
    }
?>

我只是想从我的数据库中选择一个数据并打印它。enter image description here 我得到了这个错误,很多文章都说这是我的猎物。但我已经在数据库上尝试过 quary,它运行良好。enter image description here 所以我不知道。有帮助吗?

【问题讨论】:

    标签: php sql phpmyadmin


    【解决方案1】:

    这意味着您传递的第一个参数是布尔值 (true or false)。 false 代替。 调试你的代码,例如

    // check connection is ok 
    if ( mysqli_connect_errno() ) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      exit();
    }
    

    或者你可以使用 try , catch

    try {
        $servername = "localhost";
        $username = "id20039740_antmx";
        $password = "hlA|{Px]]SZJb4^";
    
        $conn = new mysqli($servername, $username, $password);
        $sql = "select * from mst_ID";
        $result = mysqli_query($conn, $sql);
        
        while($row  = mysqli_fetch_assoc($result)) {
            echo $row["id"] . $row["status"];
        }
      //If the exception is thrown, this text will not be shown
      echo 'If you see this, your database is Ok ';
    }
    
    catch(Exception $e) {
    // and if you see this something is wrong 
      echo 'Message: ' .$e->getMessage();
    }
    

    【讨论】:

    • 谢谢你的回应!我仍然收到同样的警告,但它说我的数据库没问题。任何的想法?我很新这个
    猜你喜欢
    • 2015-04-01
    • 2012-04-21
    • 2011-01-05
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多