PHP OO 数据库无法获取 mysqli
PHP OO database couldn't fetch mysqli
我最近 运行 遇到一个数据库问题 class 我一直在使用 class 在获取 mysqli 时遇到一些问题。这里有一些 classes 在起作用,所以请耐心等待。有一个名为 Manager 的 class 连接到数据库并进行设置,然后实例化一个名为 Business 的新 class 并传入数据库,然后在代码执行中运行一个 Manager 方法,询问将一些数据保存到数据库的业务,这就是问题所在。
数据库 class 如下,敏感信息已编辑:
namespace Lib;
define('_DBHOST', 'localhost');
define('_DBUSER', '***');
define('_DBPASS', '***');
define('_DBNAME', '***');
class DB_Conn {
protected $DB;
function __construct() {
$this->connect();
}
protected function connect() {
$this->DB = new \mysqli(_DBHOST, _DBUSER, _DBPASS, _DBNAME);
if($this->DB == false) {
die(mysqli_connect_error());
}
}
function __destruct() {
$this->DB->close();
}
function getDB() {
if (!isset($this->DB)) { echo "connecting...<br />";
$this->connect();
}
return $this->DB;
}
}
这里是经理 class:
class Manager {
public $mysqli;
private $businessMan;
public function __construct() {
//Grab database and connect to it
$database = new \Lib\DB_Conn();
$this->mysqli = $database->getDB();
$this->businessMan = new \Lib\Business($this->mysqli);
}
public function submitForm($data) {
//Save Data
if($this->businessMan->addBusiness($data)) {
echo "Yay";
} else {
echo "NO!";
}
}
}
这是生意 class
namespace Lib;
class Business {
var $mysqli;
var $database;
function __construct($mysqli) {
$this->database = $mysqli;
}
function addBusiness($data)
{
$stmt = $this->database->prepare("INSERT INTO reboot_business(business_name) VALUES (?)");
//Here $stmt is NULL
//Bind the values from the array to the prepared statement
$stmt->bind_param('s', $data['values']['name']);
//Execute the prepared statement and display any error
if (!$stmt->execute()) {
var_dump($stmt->error);
//exit();
return false;
} else {
//Succcess add ID to variable
return true;
}
}
}
我仔细检查了 SQL 语句,没有问题,实际错误发生在上面的业务 class 中,我使用了 mysqli->prepare 函数。错误是:
警告:mysqli::prepare():无法获取 mysqli
致命错误:调用非对象上的成员函数 bindParam()
我还检查了 $stmt 变量,它是 NULL,所以看起来问题出在获取 mysqli
我是不是漏掉了什么,或者只是做错了什么?
我能够通过在业务构造函数中创建 DB_Conn class 的新实例来使其工作,但这似乎不正确或者我应该那样做并且经理 class 根本不负责数据库?实际上,如果管理器 class 在不需要它本身的情况下执行此 DB 操作,从 OO 的角度来看,这是错误的吗?
希望我说得有道理并感谢任何帮助。
在做了一些额外的调试后,我发现在业务构造函数中,mysqli 输出一切正常,没有错误,看起来可以正常工作。
编辑:
然而,在我的 addBusiness() 方法中,我得到了一些有趣的输出,我正在使用 var_dump($this->database) 并且我得到了大约 16 个错误:警告:var_dump(): 属性还不允许访问。
我的代码中没有任何循环在工作,这就是它出现 16 次的原因,然后当它最终运行时 var_dump 报告所有空值:
object(mysqli)#12 (19) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50010) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["stat"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL }
奇怪的是数据库 属性 在构造函数中很好,但在方法中却为空。知道为什么它会被清空或设置为空吗?
该错误是因为您正试图为您的数据库连接访问一个未设置的变量。
在您的业务构造函数中声明:
$this->database = $mysqli;
但是您正在尝试这样访问您的 MYSQLi 连接:
$stmt = $this->mysqli->prepare()
您需要引用已建立的数据库变量,因此它将是:
$stmt = $this->database->prepare()
问题是 $database 变量中的 'DB_Conn' 在管理器中的 __construct() 语句结束时超出范围.触发 DB_Conn 中的 __destruct() 关闭数据库。
答案:
将“_DB_Conn_”对象保存在 Manager->database 属性 而不是 _[ 中的 'PDO connection' =46=]_对象。
新经理class:
namespace Lib;
class Manager {
public $database = null;
private $businessMan;
public function __construct() {
//Grab database and connect to it
$this->database = new \Lib\DB_Conn();
$this->businessMan = new \Lib\Business($this->database->getDB());
}
public function submitForm($data) {
//Save Data
if($this->businessMan->addBusiness($data)) {
echo "Yay";
} else {
echo "NO!";
}
}
}
解释:
错误在'Manager'的构造函数中。
public function __construct() {
//Grab database and connect to it
$database = new \Lib\DB_Conn();
$this->mysqli = $database->getDB();
$this->businessMan = new \Lib\Business($this->mysqli);
}
class DB_Conn {
...
function __destruct() {
var_dump(__CLASS__, __METHOD__, 'this should not happen until the end!');
$this->DB->close();
}
我重新创建了您的代码并 运行 它。我收到此错误:
Couldn't fetch mysqli in P:\developer\xampp\htdocs\testmysql\Q28550480\Business.php on line 15
这让我想到了这条评论:
The cryptic "Couldn't fetch mysqli" error message can mean any number of things, including:
- 正在尝试使用已关闭的数据库...
我最近 运行 遇到一个数据库问题 class 我一直在使用 class 在获取 mysqli 时遇到一些问题。这里有一些 classes 在起作用,所以请耐心等待。有一个名为 Manager 的 class 连接到数据库并进行设置,然后实例化一个名为 Business 的新 class 并传入数据库,然后在代码执行中运行一个 Manager 方法,询问将一些数据保存到数据库的业务,这就是问题所在。 数据库 class 如下,敏感信息已编辑:
namespace Lib;
define('_DBHOST', 'localhost');
define('_DBUSER', '***');
define('_DBPASS', '***');
define('_DBNAME', '***');
class DB_Conn {
protected $DB;
function __construct() {
$this->connect();
}
protected function connect() {
$this->DB = new \mysqli(_DBHOST, _DBUSER, _DBPASS, _DBNAME);
if($this->DB == false) {
die(mysqli_connect_error());
}
}
function __destruct() {
$this->DB->close();
}
function getDB() {
if (!isset($this->DB)) { echo "connecting...<br />";
$this->connect();
}
return $this->DB;
}
}
这里是经理 class:
class Manager {
public $mysqli;
private $businessMan;
public function __construct() {
//Grab database and connect to it
$database = new \Lib\DB_Conn();
$this->mysqli = $database->getDB();
$this->businessMan = new \Lib\Business($this->mysqli);
}
public function submitForm($data) {
//Save Data
if($this->businessMan->addBusiness($data)) {
echo "Yay";
} else {
echo "NO!";
}
}
}
这是生意 class
namespace Lib;
class Business {
var $mysqli;
var $database;
function __construct($mysqli) {
$this->database = $mysqli;
}
function addBusiness($data)
{
$stmt = $this->database->prepare("INSERT INTO reboot_business(business_name) VALUES (?)");
//Here $stmt is NULL
//Bind the values from the array to the prepared statement
$stmt->bind_param('s', $data['values']['name']);
//Execute the prepared statement and display any error
if (!$stmt->execute()) {
var_dump($stmt->error);
//exit();
return false;
} else {
//Succcess add ID to variable
return true;
}
}
}
我仔细检查了 SQL 语句,没有问题,实际错误发生在上面的业务 class 中,我使用了 mysqli->prepare 函数。错误是:
警告:mysqli::prepare():无法获取 mysqli
致命错误:调用非对象上的成员函数 bindParam()
我还检查了 $stmt 变量,它是 NULL,所以看起来问题出在获取 mysqli
我是不是漏掉了什么,或者只是做错了什么?
我能够通过在业务构造函数中创建 DB_Conn class 的新实例来使其工作,但这似乎不正确或者我应该那样做并且经理 class 根本不负责数据库?实际上,如果管理器 class 在不需要它本身的情况下执行此 DB 操作,从 OO 的角度来看,这是错误的吗?
希望我说得有道理并感谢任何帮助。
在做了一些额外的调试后,我发现在业务构造函数中,mysqli 输出一切正常,没有错误,看起来可以正常工作。
编辑:
然而,在我的 addBusiness() 方法中,我得到了一些有趣的输出,我正在使用 var_dump($this->database) 并且我得到了大约 16 个错误:警告:var_dump(): 属性还不允许访问。
我的代码中没有任何循环在工作,这就是它出现 16 次的原因,然后当它最终运行时 var_dump 报告所有空值:
object(mysqli)#12 (19) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50010) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["stat"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL }
奇怪的是数据库 属性 在构造函数中很好,但在方法中却为空。知道为什么它会被清空或设置为空吗?
该错误是因为您正试图为您的数据库连接访问一个未设置的变量。
在您的业务构造函数中声明:
$this->database = $mysqli;
但是您正在尝试这样访问您的 MYSQLi 连接:
$stmt = $this->mysqli->prepare()
您需要引用已建立的数据库变量,因此它将是:
$stmt = $this->database->prepare()
问题是 $database 变量中的 'DB_Conn' 在管理器中的 __construct() 语句结束时超出范围.触发 DB_Conn 中的 __destruct() 关闭数据库。
答案:
将“_DB_Conn_”对象保存在 Manager->database 属性 而不是 _[ 中的 'PDO connection' =46=]_对象。
新经理class:
namespace Lib;
class Manager {
public $database = null;
private $businessMan;
public function __construct() {
//Grab database and connect to it
$this->database = new \Lib\DB_Conn();
$this->businessMan = new \Lib\Business($this->database->getDB());
}
public function submitForm($data) {
//Save Data
if($this->businessMan->addBusiness($data)) {
echo "Yay";
} else {
echo "NO!";
}
}
}
解释:
错误在'Manager'的构造函数中。
public function __construct() {
//Grab database and connect to it
$database = new \Lib\DB_Conn();
$this->mysqli = $database->getDB();
$this->businessMan = new \Lib\Business($this->mysqli);
}
class DB_Conn {
...
function __destruct() {
var_dump(__CLASS__, __METHOD__, 'this should not happen until the end!');
$this->DB->close();
}
我重新创建了您的代码并 运行 它。我收到此错误:
Couldn't fetch mysqli in P:\developer\xampp\htdocs\testmysql\Q28550480\Business.php on line 15
这让我想到了这条评论:
The cryptic "Couldn't fetch mysqli" error message can mean any number of things, including:
- 正在尝试使用已关闭的数据库...