当我使用stmt_init(),prepare()或query()时,我正在使用MySQLi创建一些基本的OO脚本并获取未定义的方法错误...同时使用connect_errno()获取错误。我知道我的php.ini启用了mysqli扩展(取消注释),而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;
}
所以,要使insertNode()方法起作用......这些是我放在nodeModel类中的其他方法
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范围...我不确定我应该通过什么。
答案 0 :(得分:1)
尝试使用依赖注入将连接对象传递给模型:
<?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
)
)
)
*/
?>