How to set UTF8 in php for my connection?

Hello sir, my unicode fonts are not showing and I found it required to set utf8, when already
meta charset=“utf-8” is set in the head section, why it is required in php? how I can add it here?

Class Database{
	public $host   = DB_HOST;
	public $user   = DB_USER;
	public $pass   = DB_PASS;
	public $dbname = DB_NAME;
	
	
	public $link;
	public $error;
	
	public function __construct(){
	
		$this->connectDB();
	}
	
	private function connectDB(){
	
	$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
	
	if(!$this->link){
		$this->error ="Connection fail".$this->link->connect_error;
		return false;
	}
 }
	
	// Select or Read data
	public function select($query){
	
		$result = $this->link->query($query) or die($this->link->error.__LINE__);
		if($result->num_rows > 0){
			return $result;
		} else {
			return false;
		}
	}
	
	// Insert data
	public function insert($query){
	   
	$insert_row = $this->link->query($query) or die($this->link->error.__LINE__);
	if($insert_row){
		return $insert_row;
	} else {
		return false;
	}
  }
  
    // Update data
  	public function update($query){
  	    
	$update_row = $this->link->query($query) or die($this->link->error.__LINE__);
	if($update_row){
		return $update_row;
	} else {
		return false;
	}
  }
  
  //Delete data
   public function delete($query){
      
	$delete_row = $this->link->query($query) or die($this->link->error.__LINE__);
	if($delete_row){
		return $delete_row;
	} else {
		return false;
	}
  }

 
 
}


You want mysqli::set_charset. Call it straight after you’ve created your mysqli object:

$this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
$this->link->set_charset('utf8');

Note this will only configure your connection; you must also ensure your database and PHP code is using UTF8.

1 Like

Thank you. I just needed to add changes in my database also. The table was set to latin shifted to utf_general_ci.

Sponsor our Newsletter | Privacy Policy | Terms of Service