警告:mysqli_query()期望参数1为mysqli,null为null

时间:2016-05-06 13:31:16

标签: php mysql mysqli

我正在编写简单的数据库连接,当我对其进行测试时,它无法正常工作。  我做错了什么? 文件数据库:

require_once("config.php");

class MySqlDatabase{
    private $connection;

    function __construct(){
        $this->open_connection();
    }
    public function open_connection(){
        $connection = mysqli_connect(DB_SERVER , DB_USER , DB_PASS , DB_NAME);
        if(!$connection){
            die("Database connection failed:" . mysqli_error($connection));
        }
    }
    public function query($sql){
        $result = mysqli_query($this->connection , $sql);
        $this->confirm_query($result);
        return $result;
    }
   private function confirm_query($result){
        if(!$result){
            die("Database query failed: ".mysqli_error($this->connection));
        }
    }
}

$database = new MySqlDatabase();
$db =& $database;

比我提出要求:

if(isset ($database)) {echo"true";}else {echo "false";}

$sql ="INSERT INTO users (id, username, password, first_name, last_name)";
$sql .="VALUES (1,'olegsavchuk12','1111','Oleg','Savchuk')";
$result = $database->query($sql);
$sql = "SELECT * FROM users WHERE id=1";
$result_set = $database->query($sql);
$found_user = mysqli_fetch_all($result_set);
echo $found_user['username'];

看到这个警告。

1 个答案:

答案 0 :(得分:-1)

除了省略7个简单字符之外,您的代码是干净的: $ this-> 现在,让我们重新发布您的代码,只有这次添加 $ this->

'