我在我的数据库类
中以这种方式讨论$ db对象class db{
private $connection;
public function __construct(){
$this->connect();
}
public function connect(){
$this->connection=mysqli_connect('localhost','root','','new5');
}
}
$db=new db;
我在另一个类(配置文件类)中将此$ db对象用作链接标识符
$query=$db->query("SELECT id, parent_id, name FROM categories_general");
请帮助我,我怎样才能摆脱这个错误。实际上我最近从mysql转移到了mysqli。
答案 0 :(得分:6)
试
$db = mysqli_connect('localhost','root','XXX','XXX');
答案 1 :(得分:2)
$db = new mysqli('localhost','root','','test');
$sql = "select * from user";
$result = $db->query($sql);
var_dump($result->fetch_assoc());
答案 2 :(得分:2)
你正在使用mysqli的程序风格。所以你必须在db class中创建查询方法。
class db{
private $connection;
public function __construct(){
$this->connect();
}
public function connect(){
$this->connection=mysqli_connect('localhost','root','','new5');
}
//add these methods
public function query($query_string){
return mysqli_query($this->connection, $query_string);
}
public function fetch_row($mysqli_result){
return mysqli_fetch_row($this->connection, $mysqli_result);
}
//and other functions you need
}
或者您可以使用extends方法使用面向对象的样式;
class db extends mysqli{
function __construct($host, $username, $password, $databaseName, $port=3306){
mysqli::__construct($host, $username, $password, $databaseName, $port);
}
}
答案 3 :(得分:0)
添加功能
public function getDB()
{
return $this->_mysql;
}
$query=mysqli_query($db->getDB(),"SELECT id, parent_id, name FROM categories_general");