我有几个处理数据库层的模型类,并且在如何以 OO 方式将数据库连接传递给查询方面遇到了逻辑错误。Class1 是我在 class2 查询方法的构造函数中调用的一个db_connect:
class db {
public $host = 'localhost';
public $username = 'root';
public $password = '';
public $database = 'molecule';
public $mysqli = '';
function __construct() {
$mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_errno()) {
printf("Database Connection failed: %s'n", mysqli_connect_error());
exit();
}
return $mysqli;
}
}
class nodeModel {
public $node;
public $node_name;
public $node_link;
public $node_comment;
public $mysqli;
function __construct() {
$mysqli = new db;
return $mysqli;
}
public $insert;
public $insert_id;
function insertNode($node_name, $node_link, $node_comment) {
$this->insert = $insert;
$this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
$this->insert->bind_param("sss", $this->node_name, $this->node_link, $this->node_comment);
if($this->insert->execute()) {
$this->insert_id = $mysqli->insert_id;
}
return $this->insert_id;
print_r($this->insert_id);
$this->insert->close();
}}
但是我在$insert上遇到了一个未定义的变量错误。
我用以下内容调用该层:
include 'node_model.php';
$dbTest = new nodeModel;
$node_name = 'My Node Title';
$node_link = 'My Node Link';
$node_comment = 'My Node Comment. This one should be longer so I will write more stuff';
$dbTest->insertNode($node_name, $node_link, $node_comment);
我与查询类的构造函数连接的方式似乎有效,但不确定我需要在范围内将哪个变量附加到 prepare/bind/execute 语句?
而且我知道我应该使用PDO或其他ORMy类型的工具。我会的,但这更多的是关于一些基本的 OO 获取/设置/检索最佳实践......感谢您的帮助。
您的代码存在一些主要问题...
1. 将模型类属性设为私有,并创建 setter 和 getter 方法来设置或检索属性的值。此外,您的方法插入节点是错误的。
class nodeModel {
private $node;
private $nodeName;
private $nodeLink;
private $nodeComment;
private $mysqli;
function __construct() {
$this->mysqli = new db;
}
public function getNodeName() {
return $this->nodeName;
}
public function getNodeLink() {
return $this->nodeLink;
}
public function getNodeComment() {
return $this->nodeComment;
}
public function setNodeName($value) {
$this->nodeName = $value;
}
public function setNodeLink($value) {
$this->nodeLink = $value;
}
public function setNodeComment($value) {
$this->nodeComment = $value;
}
function insertNode() {
$this->insert = $this->mysqli->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;
}}
}
2.在类db
当你创建一个连接时,将其存储到$this->mysqli
,而不仅仅是$mysqli
。
function __construct() {
$this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
if (mysqli_connect_errno()) {
printf("Database Connection failed: %s'n", mysqli_connect_error());
exit();
}
return $this->mysqli;
}
3. 在您的第三个代码中,您只创建一个变量 您传递给方法insertNode
但在此方法中您不对传递的变量执行任何操作 - 您希望设置类属性,但它们没有......因此,这样做:
include 'node_model.php';
$dbTest = new nodeModel;
$dbTest->setNodeName('My Node Title');
$dbTest->setNodeLink('My Node Link');
$dbTest->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$dbTest->insertNode();
希望这有帮助...
但是我在$insert上遇到了一个未定义的变量错误。
这是因为函数的第一行是
$this->insert = $insert;
$this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
$insert不在参数列表中,因此第一行会导致错误。然后在第二行中,无论如何都要覆盖变量。
return 语句后还有 2 行代码,当然永远不会执行。
也许是时候睡一觉了?:)