PHP - 在null上调用成员函数query() - 错误

时间:2017-03-22 13:06:09

标签: php mysql null call member

我在php中有以下代码用于连接到我的数据库:

<?php
class MY_SQL{
    private $username;
    private $password;
    private $conn;

    public function __construct($SERVERNAME){
        $this->username = "username";
        $this->password = "password";

        if($SERVERNAME == "data_"){
            $server = "Servername";
        }
        else {
            $server = $SERVERNAME;
        }

        // Create connection
        $conn = new mysqli($server, $this->username, $this->password);

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
    }

    public function SQLCommand($cmd) {
        if ( $this->conn->query($cmd) === TRUE ) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $cmd . "<br>" . $conn->error;
        }
    }
}

$sql = "INSERT INTO _test(test1, test2) VALUES ('hello','hi');";

        $database = new MY_SQL("Servername");
        $database->SQLCommand($sql);
?>

我收到以下错误:

  

致命错误:在null

上调用成员函数query()

出了什么问题?

2 个答案:

答案 0 :(得分:5)

$this->conn = $conn; in __construct()

答案 1 :(得分:3)

我建议您使用此示例(取自https://github.com/opencart/opencart/blob/master/upload/system/library/db/mysqli.php

来改进您的课程
final class My_SQLi
{
    private $connection;

    public function __construct($hostname, $username, $password, $database, $port = '3306')
    {
        $this->connection = new \mysqli($hostname, $username, $password, $database, $port);

        if ($this->connection->connect_error) {
            throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno);
        }

        $this->connection->set_charset("utf8");
        $this->connection->query("SET SQL_MODE = ''");
    }

    public function query($sql)
    {
        $query = $this->connection->query($sql);

        if (!$this->connection->errno) {
            if ($query instanceof \mysqli_result) {
                $data = array();

                while ($row = $query->fetch_assoc()) {
                    $data[] = $row;
                }

                $result = new \stdClass();
                $result->num_rows = $query->num_rows;
                $result->row = isset($data[0]) ? $data[0] : array();
                $result->rows = $data;

                $query->close();

                return $result;
            } else {
                return true;
            }
        } else {
            throw new \Exception('Error: ' . $this->connection->error . '<br />Error No: ' . $this->connection->errno . '<br />' . $sql);
        }
    }

    public function escape($value)
    {
        return $this->connection->real_escape_string($value);
    }

    public function countAffected()
    {
        return $this->connection->affected_rows;
    }

    public function getLastId()
    {
        return $this->connection->insert_id;
    }

    public function isConnected()
    {
        return $this->connection->ping();
    }

    public function __destruct()
    {
        $this->connection->close();
    }
}

$sql = "INSERT INTO _test(test1, test2) VALUES ('hello','hi');";

$mysql = new My_SQLi('host', 'user', 'password', 'db');
$result = $mysql->query($sql);