嗨,我知道如何设置“set_charset('utf8');”风格:
$db = new mysqli('127.0.0.1','root','pass','data') or die ('error with connection');
$db->set_charset('utf8');
但现在我想用以下类来做这个:
class Core {
protected $db, $result;
private $rows;
public function __construct(){
$this->db = new mysqli('127.0.0.1','root','pass','data');
}
public function query ($sql){
$this->result = $this->db->query($sql);
}
public function rows(){
for($x =1; $x<= $this->db->affected_rows; $x++){
$this->rows[] = $this->result->fetch_assoc();
}
return $this->rows;
}
}
但我无法创建这个db set charset部分,它总是出现某种错误:)
请帮忙, 谢谢
答案 0 :(得分:0)
您的代码运行正常,但在您的评论中我发现了一个错误。
试试这个解决方案:
class Core extends mysqli
{
protected $db, $result;
private $rows;
public function __construct()
{
parent::__construct('127.0.0.1','root','pass','data');
if (mysqli_connect_error()) {
throw new exception('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
$this->set_charset('utf-8');
}
public function query($sql)
{
$this->result = parent::query($sql);
}
}
当您将Core类作为Object调用时,将自动创建与数据库的连接,并将设置charset UTF-8。
扩展MySQLI类以重用所有方法,例如$ core_class_object-&gt; stmt_init();