PHP connection MySQL database garbled is usually the Chinese characters will be there, the English will not appear, because the MySQL default connection for UTF8, if your page and database encoding is not uniform, it is possible garbled.
Solutions
The page is gb2312
| The code is as follows |
Copy Code |
|
Mysql_qurey the decoding of the specified database to "set names gb2312" is to prevent Chinese garbled. |
Page encoded as Uft8
| The code is as follows |
Copy Code |
|
mysql_query (' Set NAMES UTF8 ');//Set link encoding according to PHP file encoding |
Cases
| The code is as follows |
Copy Code |
|
$connection =mysql_connect ($localhost, $username, $password);
mysql_query (' Set NAMES UTF8 ');//Set link encoding according to PHP file encoding
GBK words
mysql_query (' SET NAMES gbk ');
$db _selected = mysql_select_db ($database, $connection);
if (! $db _selected) {
Die (' Can ' t use DB: '. mysql_error ());
} |
This can solve the output data will not be garbled, if you save the data garbled people as far as possible, the page encoding and database field encoding consistent can be resolved.
http://www.bkjia.com/PHPjc/632172.html www.bkjia.com true http://www.bkjia.com/PHPjc/632172.html techarticle PHP Connection MySQL database garbled is usually the Chinese characters will be there, the English will not appear, because the MySQL default connection is UTF8, if your page and database encoding is not uniform, it is possible ...