Perfect solution to the problem of storing garbled characters in Php+mysql
Preface: PHP and MySQL database Chinese character coding problems.
1. mysql Database side
The database encoding method is set uniformly to utf8_general_ci
2, the encoding method in the PHP file
To insert data into a database, for example, the code example is as follows:
<?PHP$conn=Mysqli_connect("127.0.0.1", "root", "123");if(!$conn){ die(' Cannot connect to database '.Mysql_error());}Mysqli_query($conn, "SET NAMES GBK");mysqli_select_db($conn, "Hanyu");Mysqli_query($conn,"INSERT into Test (id,name) VALUES (' 6 ', ' Piggy ')");Mysqli_close($conn);?>
3. Summary
Coding problem is really a headache, small series now finally the perfect solution, really happy. It is also hoped that this article will be helpful to friends who have the same coding problems.
Perfect solution to the problem of storing garbled characters in Php+mysql