【问题标题】:mysqli_query() expects parameter 1 to be mysqli, null given in mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in [duplicate]mysqli_query() 期望参数 1 为 mysqli,mysqli_fetch_array() 中给出的 null 期望参数 1 为 mysqli_result,[重复] 中给出的 null
【发布时间】:2018-07-01 06:04:59
【问题描述】:

我的错误信息在这里

mysqli_query() 期望参数 1 为 mysqli,第 28 行给出 null 并且 mysqli_fetch_array() 期望参数 1 为 mysqli_result, null 在第 29 行给出

我的代码

    <?php

    class Database{
    public $con;
    public $error;
    
    public function _construct(){
        $this->con=mysqli_connect("localhost","root","","db");
        if(!$this->con)
        {
            echo "Database Connection Error" .mysqli_connect_error($this->con);
        }
    
    }
    public function login($data)
    {
        $username =$data['username'];
        $password =$data['password'];
        
        if($username=="" || $password=="")
        {
            $msg="Field must not be empty";
            return $msg;
        }
        else
        {
            $query="SELECT * FROM tbl_table where username = '$username' AND 
            password = '$password'";
            $result=mysqli_query($this->con,$query);
            $row=mysqli_fetch_array($result);
            $user = $row['username'];
            $pass = $row['password'];
            
            if($username==$user && $password==$pass)
            {
                header("Location: dashboard.php");
            }
            else
            {
                $msg="Username and Password not match";
                return $msg;
            }               
        }
    }
}
    ?>

【问题讨论】:

  • 能否提供var_dump(mysqli_connect("localhost","root","","db"));的输出?
  • 检查您的查询。不需要在变量前后加上 ' ' 用双引号括起来。因为在双引号中,它会自动解析字符串。
  • 谢谢@ParthPatel,但我仍然收到错误

标签: php oop mysqli


【解决方案1】:

你有一个错字。应该是__construct() 注意额外的_

【讨论】:

    【解决方案2】:

    在 mysqli_fetch_array() 中传递另一个参数,第二个参数是您要获取的数组类型(MYSQLI_ASSOC)。所以让它

    mysqli_fetch_array($result,MYSQLI_ASSOC);
    

    或者另一个原因可能是您的 mysqli_query 没有返回任何内容。这可能是因为您的查询代码中有一些错误。尝试在您的 mysql 数据库中进行该查询,看看它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多