php reads a simple instance of mysql

  • 2020-12-10 00:39:53
  • OfStack

    $link=mysql_connect("localhost","root"," Previous administrator password ");
    if(!$link) echo " Failed connection !";
    mysql_select_db("infosystem", $link); // Select database 
    $q = "SELECT * FROM info"; //SQL The query 
    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 The name of the </td></tr>";
    while($row = mysql_fetch_array($rs)) echo "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>"; // Display the data 
    echo "</table>";
    mysql_free_result($rs); // Close data set 

Chinese shows the problem of garbled code
When we access the MySQL database the way we did before, even if we set the default character set of the table to utf8 and send the query through UTF-8, you will still find that the database is still a mess.
The easy way to do this is via phpMyAdmin.
Set the following items:
1: Language set to chinese (ES14en-utf-8)
MySQL Character set: UTF-8 Unicode (utf8)
3: MySQL connection proofread: utf8_general_ci
4: When adding database and data table, select utf8_general_ci
With the above Settings, Chinese characters will not be scrambled when operating and querying in phpMyAdmin.
However, you will find that in the php program, the result of the query using the previous sql statement is still confused, and the problem is at 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);
mysql_query("SET NAMES 'utf8'", LINK);

gbk coding

$mysql_mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
mysql_query("SET NAMES 'GBK'");

Related articles: