Copy Code code 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 Data Set
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); Closing datasets
?>
Chinese display garbled problem
When we access the MySQL database through PHP in the way we used to, even if we set the default character set of the table to UTF8 and send the 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 the new database and datasheet, the collation of the selection utf8_general_ci
Through the above settings, in the phpMyAdmin operation 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 statement after the database is successfully connected:
Copy Code 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.
Copy Code code as follows:
$mysql _mylink = mysql_connect ($mysql _host, $mysql _user, $mysql _pass);
mysql_query ("SET NAMES ' GBK '");