警告:mysqli_query()要求参数1为mysqli,在第19行的Operations.php中给出null

时间:2016-12-09 17:33:38

标签: php mysqli

我有Connection类和Operations类 当调用用于将记录插入数据库的必需方法时,显示错误。

文件名:Connection.php

sfbg

用于建立连接的连接方法

<?php
/**
* Connection class to make "connection" with database
*/
class Connection  
{   
    public $host;
    public $user;
    public $password;
    public $databaseName;

    /**
    * Constructor
    */
public  function __construct()
    {   
        $this->host = '';
        $this->user = '';
        $this->password = '';
        $this->databaseName = '';

        $this->connect();
    }
    /**
    * The method that make the connection to the database
    */

名称:Operations.php

    public function connect()
    {
        $con = mysqli_connect($this->host, $this->user, $this->password, $this->databaseName);
        if(!$con){
            echo "Connect to the database failed";
        }

        return $con;
    }

}

当我调用以下函数时,它会显示错误

 require_once"Connection.php";

    class Operations
    {
        //$Connection = new Connection();
        //$con = $Connection->connect();
        function __construct()
        {
            $Connection = new Connection();
            $con = $Connection->connect();
        }

1 个答案:

答案 0 :(得分:0)

更改 Connection.php ,如下所示,

<?php
class Connection  
{   
    public $host;
    public $user;
    public $password;
    public $databaseName;

public  function __construct($host,$user,$password,$database)
    {   
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->databaseName = $database;

        $this->connect();
    }

现在更改 Operations.php文件,如下所示

 require_once"Connection.php";
    class Operations
    {
        function __construct()
        {
            $Connection = new Connection("host","user","passwrd","database");
            $con = $Connection->connect();
        }