PHP MySQLi 未定义方法错误


PHP MySQLi Undefined Method Error

我正在使用MySQLi创建一些基本的OO脚本,并在使用stmt_init(),prepare()或query()中的任何一个时出现未定义方法错误...还收到 connect_errno() 错误。我确实知道mysqli扩展在我的php中已启用(未注释.ini并且phpinfo()同时启用了mysqli和mysqlnd...所以不确定为什么我无法访问方法/属性。我得到的错误是:致命错误:调用未定义的方法 mysqli::connect_error()

class db {
    public $host = 'localhost';
    public $username = 'root';
    public $password = '';
    public $database = 'molecule';
    public $mysqli = '';
    function __construct() {
        $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
    return $this->mysqli;
    }   
}
class nodeModel {
    function __construct() {
        $this->mysqli = new db;
        if($this->mysqli->connect_error()){ printf("Database Connection failed: %s'n", $this->mysqli->connect_error()); }
    }
    function insertNode() {
        $this->insert = $this->mysqli->stmt_init();
        $this->insert->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->insert->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->insert->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->insert->close();
        print_r($this->insert_id);
        return $this->insert_id;
    }

因此,要使插入节点()方法工作...这些是我放入节点模型类中的其他方法

public function setNodeName($value) { $this->nodeName = $value; }
public function setNodeLink($value) { $this->nodeLink = $value; }
public function setNodeComment($value) { $this->nodeComment = $value; }
public function getNodeName() { return $this->nodeName; }
public function getNodeLink() { return $this->nodeLink; }
public function getNodeComment() { return $this->nodeComment; }
public $insert_id;
function insertNode($nodeName, $nodeLink, $nodeComment) {
        $this->mysqli->stmt_init();
        $this->mysqli->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->mysqli->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->mysqli->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->mysqli->close();
        print_r($this->insert_id);
        return $this->insert_id;
    }

如何将变量传递到方法中?我正在尝试这个...

$connect = new db();
$db = new nodeModel($connect);
$db->setNodeName('My Node Title');
$db->setNodeLink('My Node Link');
$db->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$db->insertNode($db->getNodeName(), $db->getNodeLink(), $db->getNodeComment());

但这行不通。我的困惑实际上是关于类方法内部的 OO 范围......我不确定我应该通过什么。

试试这个,使用依赖注入将连接对象传递给模型:

<?php 
class db{
    protected $mysqli;
    function __construct($host,$username,$password,$database) {
        if(!$this->mysqli instanceof mysqli){
            $this->mysqli = new mysqli($host, $username, $password, $database);
            if ($this->mysqli->connect_errno) {
                die("Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error);
            }
        }
    }
}
class nodeModel{
    function __construct($connection) {
        $this->mysqli = $connection;
    }
    function status() {
        return print_r($this,true);
    }
}
//Create the database object
$connect = new db('localhost','root','password','db');
//Inject the database object into the model
$db = new nodeModel($connect);
//Example method inside the model class
print_r($db->status());
/*
nodeModel Object
(
    [mysqli] => db Object
        (
            [mysqli:db:private] => mysqli Object
                (
                    [affected_rows] => 0
                    [client_info] => mysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $
                    [client_version] => 50008
                    [connect_errno] => 0
                    [connect_error] => 
                    [errno] => 0
                    [error] => 
                    [field_count] => 0
                    [host_info] => localhost via TCP/IP
                    [info] => 
                    [insert_id] => 0
                    [server_info] => 5.5.16
                    [server_version] => 50516
                    [sqlstate] => 00000
                    [protocol_version] => 10
                    [thread_id] => 28
                    [warning_count] => 0
                )
        )
)
*/
?>