我有这个数据库类连接到MySQL:
private $db_host;
private $db_user;
private $db_pass;
private $db_name;
function __construct($db_host, $db_user, $db_pass, $db_name) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_name = $db_name;
}
public function connect(){
if(!$this->con){
$this->myconn = new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name);
if($this->myconn->connect_errno > 0){
array_push($this->result,$this->myconn->connect_error);
return false;
}else{
$this->con = true;
return true;
}
}else{
return true;
}
}
我将$db_server, $db_user, $db_password, $db_name
存储到config.php
文件中。
每当我想连接到数据库时,我都会使用以下代码:
require_once ($_SERVER['DOCUMENT_ROOT'] . '/config.php');
$product = new Product($db_server, $db_user, $db_password, $db_name);
$product->connect();
不幸的是,那段代码产生了这个错误日志:
[06-Jan-2017 12:37:50 Asia/Jakarta] PHP Warning: mysqli::mysqli() expects parameter 2 to be string, object given in /home/**/public_html/model/Database.class.php on line 39
第39行包含:
$this->myconn = new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name);
为什么$db_server, $db_user, $db_password, $db_name
变量标记为对象?如何将其更改为字符串?这样错误就不会再出现了。