Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PDO and UTF-8 Special characters in PHP / MySQL?
- $dbhost = 'localhost';
- $dbuser = 'root';
- $dbpass = '';
- $con = mysql_connect("localhost","root","");
- mysql_set_charset('utf8');
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("kdict", $con);
- $sql = "SELECT * FROM `en-kh` where english='a'";
- echo $sql;
- $result = mysql_query($sql);
- while($row = mysql_fetch_array($result))
- {
- echo $row['english'] . " </br> " . $row['khmer'];
- echo "<br />";
- }
- ?>
- class crud {
- //code..
- public function conn()
- {
- isset($this->username);
- isset($this->password);
- if (!$this->db instanceof PDO)
- {
- $this->db = new PDO($this->dsn, $this->username, $this->password);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
- }
- }
- /*more code here*/
- }
- /*** a new crud object ***/
- $crud = new crud();
- /*** The DSN ***/
- $crud->dsn = "mysql:dbname=kdict;host=localhost";
- /*** MySQL username and password ***/
- $crud->username = 'root';
- $crud->password = '';
- /*** select all records from table ***/
- $records = $crud->rawSelect("SELECT * FROM `en-kh` where english='a'");
- /*** fetch only associative array of values ***/
- $rows = $records->fetchAll(PDO::FETCH_ASSOC);
- /*** display the records ***/
- foreach($rows as $row)
- {
- foreach($row as $fieldname=>$value)
- {
- echo $fieldname.' = '.$value.'<br />';
- }
- echo '<hr />';
- }
- ?>
- $this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
- $this->db = new PDO($this->dsn, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
- $this->db = new PDO($this->dsn, $this->username, $this->password);
- $this->db->exec("SET NAMES 'utf8';");
Add Comment
Please, Sign In to add comment