在类中将数据库对象从一个方法传递到另一个方法的正确方法是什么

时间:2012-09-16 11:36:25

标签: php mysql parameter-passing

我需要帮助理解将一个方法中创建的对象(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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您没有在任何地方设置$this->mysqli,您需要在构造中执行$this->mysqli = $mysqli;而不是return $this->mysqli

答案 2 :(得分: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;

}

答案 3 :(得分: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( ... );这样的数据库时,您可以检查错误属性,然后决定要做什么。