Home  >  Article  >  Backend Development  >  How to set mysql encoding in php

How to set mysql encoding in php

藏色散人
藏色散人Original
2020-12-02 09:41:162834browse

php method to set mysql encoding: first connect to the database through the "mysql_connect" function; then set the encoding method to utf8 through the "mysql_query("set names utf8");" method.

How to set mysql encoding in php

The operating environment of this tutorial: Windows 7 system, PHP 7.1 version, Dell G3 computer. This method is suitable for all brands of computers.

Recommended: "PHP Video Tutorial"

PHP Set the encoding method when connecting to the Mysql database (one of the causes of garbled characters)

Directly upload the code:

Connect to the database:

<?php
   $connect=mysql_connect("服务器","登录的用户名","登录的密码");  
   mysql_select_db("数据库名",$connect);
   mysql_query("set names utf8");   //这里设置编码方式为 utf8 
?>

ps: When I set the encoding method, I wrote mysql_query("set names utf-8"); Use wamp encoding locally , there was no problem with the display. Then when I put the system I made on Wanwang's virtual host, garbled characters appeared. Some Chinese characters could be displayed, while others were not? Waiting for weird characters, I checked that the encoding method of the PHP web page file and the source code are both utf-8. Finally, I found that it was a problem when setting the encoding for the database connection. I struggled with it for a long time, and... ..

The above is the detailed content of How to set mysql encoding in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn