Php getting started learning knowledge point 1 connection and query between PHP and MYSql _ php getting started _ script house

Source: Internet
Author: User
Php getting started Knowledge Point 1: Connection and query between PHP and MYSql. The most important thing to learn about php is the interaction with the database.

Php getting started Knowledge Point 1: Connection and query between PHP and MYSql. The most important thing to learn about php is the interaction with the database.

1. Connect to the database
1. mysql_connect: Enable the MySQL link.

Mysql_connect ('host', 'username', 'Password ')

2. mysql_select_db: open a database
Mysql_select_db ('database name', $ link identifier) // if the link identifier is left blank, the last opened connection is used by default.

3. mysql_query ("set names 'gbk'") solves Chinese garbled characters;

Mysql_query ("set names 'Encoding (utf8 or GBK)'") // UTF8 cannot contain "-"

Ii. query data

1. mysql_query (SQL statement, connection identifier );

$ SQL = "Select * FROM Test"
$ Result = mysql_query ($ SQL) // The Connection identifier is the last opened link by default.

// Get error information

$ Result = @ mysql_query ($ SQL) or die (mysql_error ())

2. Obtain the query results.

A. mysql_fetch_row ($ result );

$ Row = mysql_fetch_row ($ result );
Echo $ row [0];

B. mysql_fetch_array ($ result );

$ Row = mysql_fetch_array ($ result );
Echo $ row [0];
Echo $ row ['key'];


Note: The functions of mysql_fetch_array and mysql_fetch_row are basically the same, except that they can be indexed at the offset starting from 0, or by domain name.
Returns all the domain values of the next row and saves them to an array. If no row exists, false is returned.

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.