PHP read a simple example of MySQL

Source: Internet
Author: User
Tags character set mysql database phpmyadmin

Read the database we need to use the functions in PHP to connect to the database and then use the data in the SQL query table to iterate through it again, let's look at a simple read MySQL instance

The code is as follows: <?php     $link =mysql_connect ("localhost", "root", "previous admin password");     if (! $link) echo "No connection succeeded!";     mysql_select_db ("Infosystem", $link); Select Database     $Q = "SELECT * from Info"; SQL query Statement     mysql_query ("SET NAMES GB2312");     $rs = mysql_query ($q); Get DataSet     if (! $rs) {die ("Valid result!");}     echo "<table>";     echo "<tr><td> Department name </td><td> employee name </TD><TD>PC name </td></tr > ";     while ($row = Mysql_fetch_array ($rs)) echo "<tr><td> $row [1]</td><td> $row [2]< /td><td> $row [3]</td></tr> "; Display data     echo "</table>";     Mysql_free_result ($RS); Close DataSet?>                  Chinese display garbled problem when we access the MySQL database through PHP in the same way, even if we set the default table Character set for UTF8 and send a query via UTF-8 encoding, you will find that the database is still garbled. In fact, the simple way is to set by phpMyAdmin. Set the following: 1: Language set to Chinese (zh-utf-8) 2:MySQL Character Set: UTF-8 Unicode (UTF8) 3:mysql connection proofing: Utf8_general_ci 4: When new databases and datasheets are added, the collation selection utf8_general_ci through the above settings, operates in the phpMyAdmin and query, the Chinese characters are not garbled. But you will find that in the PHP program with the previous SQL statements query results or garbled, the problem is on the connection connection layer. The workaround is to send a query after the database is successfully connected:     Code as follows: 1: $this->linkid = mysql_connect ($this->host, $this->user, $ This->password); 2:mysql_query (' SET NAMES ' UTF8 ', $this->linkid); Or: DEFINE (' LINK ', mysql_connect (Db_host, Db_user, Db_password)); mysql_query ("SET NAMES ' UTF8 '", LINK);     GBK encoded     code is as follows: $mysql _mylink = mysql_connect ($mysql _host, $mysql _user, $mysql _pass); mysql_query ("SET NAMES ' GBK '");  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.