Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf 8 - PHP and MySQLi UTF8 [duplicate]

my table char set is utf8 and it's collation is utf8.now i have this code:

   $mysqli = new mysqli("localhost", "root", "", "Amoozeshgah");              if (mysqli_connect_errno()) {                   printf("Connect failed: %s\n", mysqli_connect_error());              }           if (!$mysqli->set_charset("utf8")) {     printf("Error loading character set utf8: %s\n", $mysqli->error); } else {     printf("Current character set: %s\n", $mysqli->character_set_name()); }         mysql_set_charset('utf8');             if ($stmt = $mysqli->prepare("SELECT About_Title FROM Tbl_About WHERE About_Id=?")) {                 $city = 8;                 /* bind parameters for markers */                 $stmt->bind_param("s", $city);                /* execute query */                 $stmt->execute();                 /* bind result variables */                    $result = $stmt->get_result();               /* fetch value */             while ($myrow = $result->fetch_assoc()) {          // use your $myrow array as you would with any other fetch         printf("%s is in district %s\n", $city, $myrow['About_Title']);         print("shod");      } 

but out put is:

Current character set: utf8 8 is in district نتمنتشس shod 

what can i do? Edit: i replaced:

if (!$mysqli->set_charset("utf8")) {         printf("Error loading character set utf8: %s\n", $mysqli->error);     } else {         printf("Current character set: %s\n", $mysqli->character_set_name());     }             mysql_set_charset('utf8'); 

with

$mysqli->set_charset("utf8") 

but no difference.

like image 995
Mahdi_Nine Avatar asked Apr 26 '12 10:04

Mahdi_Nine


People also ask

Which is better UTF-8 or utf8mb4?

The difference between utf8 and utf8mb4 is that the former can only store 3 byte characters, while the latter can store 4 byte characters. In Unicode terms, utf8 can only store characters in the Basic Multilingual Plane, while utf8mb4 can store any Unicode character.

What is the difference between utf8mb4 and UTF-8 charsets in MySQL?

utf-8 can store only 1, 2 or 3 bytes characters, while utf8mb4 can store 4 bytes characters as well. utf-8 is a subset of characters given by utf8mb4 .

What is UTF-8 PHP?

Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.


2 Answers

Please replace mysql_set_charset('utf8'); to $mysqli->set_charset("utf8") :-)

like image 82
Rohit Choudhary Avatar answered Sep 21 '22 07:09

Rohit Choudhary


or mysqli_set_charset($this->mysqli,"utf8"); mysqli_set_charset($conn,"utf8"); 
like image 39
Rinzler Avatar answered Sep 20 '22 07:09

Rinzler