Php 我正在将代码mysql更新为mysqli,现在我陷入了is_资源方法
我花了将近一天的时间来找出一个与is_资源方法相关的错误。这是我的代码Php 我正在将代码mysql更新为mysqli,现在我陷入了is_资源方法,php,Php,我花了将近一天的时间来找出一个与is_资源方法相关的错误。这是我的代码 function __construct() { $this->dbHost = "localhost"; $this->dbUser = "root"; $this->dbPass = ""; $this->dbName = "tahseent_digitemb"; } public function connect() { $this->dbh = m
function __construct() {
$this->dbHost = "localhost";
$this->dbUser = "root";
$this->dbPass = "";
$this->dbName = "tahseent_digitemb";
}
public function connect() {
$this->dbh = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass)
;6 or die("Could not connect to the database:<br />" . mysqli_error($this->dbh));
mysqli_select_db($this->dbh, $this->dbName)
or die("Database error:<br />" . mysqli_error($this->dbh));
if(!is_resource($this->dbh)) {
trigger_error("Unable to Connect to the Database Server: ".$this->dbHost, E_USER_ERROR);
//die("Error:: Unable to Connect to the Database Server.");
}
if(!mysqli_select_db($this->dbh, $this->dbname)) {
trigger_error("Unable to Select Database:: ".$this->dbname, E_USER_ERROR);
//die("Error:: Unable to Select Database");
}
函数uuu构造(){
$this->dbHost=“localhost”;
$this->dbUser=“root”;
$this->dbPass=“”;
$this->dbName=“tahseent\u digitemb”;
}
公共功能连接(){
$this->dbh=mysqli\u connect($this->dbHost,$this->dbUser,$this->dbPass)
;6或die(“无法连接到数据库:
”。mysqli_错误($this->dbh));
mysqli_select_db($this->dbh,$this->dbName)
或者死(“数据库错误:
”。mysqli_错误($this->dbh));
如果(!是_资源($this->dbh)){
触发器_错误(“无法连接到数据库服务器:”..this->dbHost,E_USER_错误);
//die(“错误::无法连接到数据库服务器。”);
}
如果(!mysqli_select_db($this->dbh,$this->dbname)){
触发器_错误(“无法选择数据库::”$this->dbname,E_USER_错误);
//die(“错误::无法选择数据库”);
}
我正在成功连接到我的数据库,因为我的连接没有中断,但我可以
我不理解为什么资源方法显示错误的结果。请帮助是类构造函数的别名,因此$this->dbh
不是资源而是对象。适当的测试应该是使用is\u object
或者mysqli的实例
也就是说,没有必要进行测试。您最初的或死测试将已经捕获到任何错误。MySQLI不使用资源,它使用对象。MySQLI\u connect
返回类MySQLI
的对象,MySQLI\u prepare
返回类MySQLI\u stmt
的对象,以及MySQLI\u querySELECT
查询时,ode>返回类为mysqli\u result
的对象
如果失败,所有这些函数都将返回false
。与其测试返回值是否是资源,不如测试它是否真实:
$this->dbh = mysqli_connect(...);
if ($this->dbh) {
// successfull
} else {
// report error
}