Php 如何设置;set#u字符集(';utf8';)&引用;对于整个班级:
嗨,我知道如何设置“set_charset('utf8');”的样式:Php 如何设置;set#u字符集(';utf8';)&引用;对于整个班级:,php,database,class,character-encoding,Php,Database,Class,Character Encoding,嗨,我知道如何设置“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(){
$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,$result;
私人$行;
公共函数构造(){
$this->db=newmysqli('127.0.0.1','root','pass','data');
}
公共函数查询($sql){
$this->result=$this->db->query($sql);
}
公共函数行(){
对于($x=1;$xdb->受影响的_行;$x++){
$this->rows[]=$this->result->fetch_assoc();
}
返回$this->rows;
}
}
但是我不能创建这个db集字符集部分,它总是有某种错误:)
请帮忙,
谢谢您的代码运行良好,但在您的评论中我发现了一个bug 尝试以下解决方案:
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);
}
}
当您将核心类作为对象调用时,将自动创建与数据库的连接,并将其设置为字符集UTF-8
MySQLI类被扩展为重用所有方法,比如$core_class_object->stmt_init() 这到底是什么样的错误?发布的代码没有错误,它工作正常。问题是当我尝试插入“set_charset('utf8');”部分时。如果我尝试使用
$this->db=newmysqli('127.0.0.1','root','pass','data')代码>使用:$this->set_字符集('utf8')代码>它给出了“致命错误:调用未定义的方法Homepage::set_charset()”