PHP Simple Read example of MySQL _php tutorial

Source: Internet
Author: User
Tags administrator password
Read MySQL Database

Cases.

$link =mysql_connect ("localhost", "root", "previous Administrator password");
if (! $link) echo "Not connected successfully!";
mysql_select_db ("Infosystem", $link); Select Database
$q = "SELECT * from Info"; SQL query Statements
mysql_query ("SET NAMES GB2312");
$rs = mysql_query ($q); Get Data Set
if (! $rs) {die ("Valid result!");}
echo "




"; echo " "; while ($row = Mysql_fetch_array ($rs)) echo " "; Show Data echo "
Department Name Employee Name 111cn.net PC Name
$row [1] $row [2] $row [3]
";
Mysql_free_result ($RS); To close a data set
?>

For more detailed information, please see: http://www.bKjia.c0m/phper/php-database/57069.htm

Chinese display garbled problem

When we access the MySQL database by PHP in the original way, even if the default character set of the table is UTF8 and the query is sent through UTF-8 encoding, you will find that the database is still garbled.
In fact, the simplest way is to set it through 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 adding database and data table, organize item 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 statement 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:

Code to copy 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 to copy code as follows
$mysql _mylink = mysql_connect ($mysql _host, $mysql _user, $mysql _pass);
mysql_query ("SET NAMES ' GBK '");

http://www.bkjia.com/PHPjc/699423.html www.bkjia.com true http://www.bkjia.com/PHPjc/699423.html techarticle Read the MySQL database example. PHP $link =mysql_connect ("localhost", "root", "previous Administrator password"), if (! $link) echo "Not connected successfully!"; mysql_select_db ("Infosystem", $link); Choose ...

  • 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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.