<?php
$link=mysql_connect("localhost","root","previous administrator password");
if(!$link) echo "No connection was successful!";
mysql_select_db("infosystem", $link); //Select database
$q = "SELECT * FROM info"; //SQL query statement
mysql_query("SET NAMES GB2312");
$rs = mysql_query($q); //Get the 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>"; //Show data
echo "</table>";
mysql_free_result($rs); //Close the dataset
?>
Chinese garbled display
When we access the MySQL database through PHP in the original way, even if the default character set of the table is set to utf8 and the query is sent through UTF-8 encoding, you will find that the stored database is still garbled.
In fact, the simple method is to set it through phpMyAdmin.
Set the following items:
1: Language is set to chinese (zh-utf-8)
2: MySQL character set: UTF-8 Unicode (utf8)
3: MySQL connection proofreading: utf8_general_ci
4: When adding a database and data table, select utf8_general_ci
Through the above settings, Chinese characters will no longer be garbled when operating and querying in phpMyAdmin.
But you will find that the results of querying using previous SQL statements in PHP programs are still garbled, and the problem lies in the connection connection layer.
The solution is to send a query statement after successfully connecting to the database:
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
$mysql_mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_query("SET NAMES 'GBK'");