Php for mysql simple read instances, phpmysql instance _ PHP Tutorial

Source: Internet
Author: User
Php is a simple mysql read instance. Php provides a simple mysql read instance, while a phpmysql instance reads a mysql database instance. The code is as follows: Copy the code? Php $ linkmysql_connect (localhost, root, previous administrator password); if (php instance for simple mysql reading, phpmysql instance

Read mysql database

Example.

The code is as follows:
$ Link = mysql_connect ("localhost", "root", "previous administrator password ");
If (! $ Link) echo "no connection successful! ";
Mysql_select_db ("infosystem", $ link); // select a database
$ Q = "SELECT * FROM info"; // SQL query statement
Mysql_query ("set names GB2312 ");
$ Rs = mysql_query ($ q); // Obtain a dataset
If (! $ Rs) {die ("Valid result! ");}
Echo"




";Echo" ";While ($ row = mysql_fetch_array ($ rs) echo" "; // Display DataEcho"
Department name Employee Name PC name
$ Row [1] $ Row [2] $ Row [3]
";
Mysql_free_result ($ rs); // Close the dataset
?>

Garbled characters in Chinese

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 the UTF-8 encoding, you will find that the database is still garbled.
In fact, you can set a simple (www.111cn.net) ticket through phpMyAdmin.
Set the following items:

1: language set to chinese (zh-utf-8)

2. MySQL character set: UTF-8 Unicode (utf8)
3: MySQL connection verification: utf8_general_ci
4: When adding a database and a data table, select utf8_general_ci as the sorting item.

Through the above settings, Chinese characters are not garbled during operations and queries in phpMyAdmin.
However, you will find that the results obtained by using the previous SQL statement in the php program are garbled, and the problem lies in the connection layer.

The solution is to send a query statement after successfully connecting to the database:

The code is 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

The code is as follows:
$ Mysql_mylink = mysql_connect ($ mysql_host, $ mysql_user, $ mysql_pass );
Mysql_query ("set names 'gbk '");

From: http://www.111cn.net/phper/php-database/57069.htm


A simple example of database insertion/reading in PHP





Untitled Document </title>
</Head>



If (isset ($ _ POST ['TJ ']) // if you click submit
{

$ Conn = mysql_connect ("localhost", "username", "password ");
Mysql_select_db ("eastses", $ conn );
Mysql_query ("set names utf8", $ conn );
Date_default_timezone_set ("asia/chongqing ");
$ SQL = "INSERT INTO 'asts '. 'classmat' ('id', 'name', 'nickname', 'birthday', 'home', 'Blood ', 'QQ', 'Weibo', 'Email ', 'phone ', 'hoby', 'food', 'sentence', 'giput', 'Lover ', 'keenon', 'Unforgettable', 'wanttobecome', 'ideal ', 'Other') VALUES ('". $ _ POST ['id']. "','". $ _ POST ['name']. "','". $ _ POST ['nickname']. "','". $ _ POST ['birthday']. "','". $ _ POST ['home']. "','". $ _ POST ['blood ']. "','". $ _ POST ['QQ'] ...... remaining full text>

Example of writing php to a database

If your database is set up, it will be easy to use PHP to connect to your database!
$ Link = mysql_connect ('localhost', 'user', 'pwd'); // your database username and password
Mysql_query ('set names utf8'); sets the character set
Mysql_select_db ('DB'); // select your database
$ SQL = "here is your SQL statement ";
Mysql_query ($ SQL); // sends an SQL statement
Mysql_close (); // close the connection

Slave reads the mysql database instance. The code is as follows? Php $ link = mysql_connect ("localhost", "root", "previous administrator password"); if (...

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.