1

我需要帮助来理解将在一种方法中创建的对象(MySQL 连接)传递给同一类中的另一种方法的正确方法。

我正在尝试创建一个数据库类,它使用构造函数和所有其他方法加载连接,使用这个连接来执行它们各自的功能。

在尝试这样做时,我不断收到此错误:

致命错误:在第 44 行的 /database.php 中的非对象上调用成员函数 query()

我的代码:

class database
{ 
    public $mysqli;

    public function __construct($database_server, $database_name, $database_user, $database_pass)
    {
        $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

        if ($mysqli->connect_error)
        {
                  die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
        }
        echo "Database connection established successfully.<br>";
        return $this->mysqli;

    }

    public function fetch_data()
    {

        $query = "select * from payment";
        if ($result = $this->mysqli->query($query))
        {

            // fetch associative array
            while ($row = $result->fetch_assoc())
            {
                printf ("%s (%s)\n", $row["id"], $row["status"]);
            }

        }

    }
}

任何帮助,将不胜感激。谢谢大家。

4

4 回答 4

1
public function __construct($database_server, $database_name, $database_user, $database_pass)
{
    $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

    if ($mysqli->connect_error)
    {
              die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
    }
    echo "Database connection established successfully.<br>";

    // instead of return, just set the local variable to the `mysqli` property.
    $this->mysqli = $mysqli;

}

或者

public function __construct($database_server, $database_name, $database_user, $database_pass)
{
    $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

    if ($this->mysqli->connect_error)
    {
       die('Connect Error (' . $this^>mysqli->connect_errno . ') '. $this->mysqli->connect_error);
    }
    echo "Database connection established successfully.<br>";    
}

并且public $mysqli;会更好protected

于 2012-09-16T11:38:33.127 回答
1

你没有$this->mysqli在任何地方设置,你需要做$this->mysqli = $mysqli;而不是return $this->mysqli在你的构造中。

于 2012-09-16T11:38:50.197 回答
0

您实际上并没有创建 $mysqli 作为类的属性。

你需要:

class database
{

public $mysqli;

public function __construct($database_server, $database_name, $database_user, $database_pass)
{
    $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

    if ($this->mysqli->connect_error)
    {
              die('Connect Error (' . $this->mysqli->connect_errno . ') '. $this->mysqli->connect_error);
    }
    echo "Database connection established successfully.<br>";
    return $this->mysqli;

}

或者

class database
{

public $mysqli;

public function __construct($database_server, $database_name, $database_user, $database_pass)
{
    $mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

    if ($mysqli->connect_error)
    {
              die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
    }
    echo "Database connection established successfully.<br>";
    $this->mysqli = $mysqli;
    return $this->mysqli;

}
于 2012-09-16T11:38:52.067 回答
0

正如其他人已经提到的那样,您应该像这样设置mysqli:

$this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

我建议不要死在构造函数中,这不是由数据库类决定,而是由数据库类的用户决定。像这样改变它:

class database
{ 
    private $mysqli;
    public $error = false;

    public function __construct($database_server, $database_name, $database_user, $database_pass)
    {
        $this->mysqli = new mysqli($database_server,$database_user, $database_pass, $database_name);

        if ($mysqli->connect_error)
          $this->error = 'Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);

    }

像这样构建数据库时,$db = new Database( ... );您可以检查错误属性,然后决定要做什么。

于 2012-09-16T11:47:25.360 回答