Application of MySQL in PHP5

Source: Internet
Author: User

1. Establish a link between PHP and mysql

Php. ini load mysql components:
Before extension = php_mysql.dll; remove
Extension_dir = "" is the path correct?

Link PHP to mysql Functions

Mysql_connect: Enable the MySQL Link
Mysql_select_db: open a database

@ And or die hide error and condition display

Mysql_connect ("host", "User Name", "password ");

Mysql_select_db ("Open Database", connection identifier );

2. How to execute an SQL statement

Mysql_query (SQL statement, connection identifier );

Note: mysql_query is used to send queries to the current database of the database server based on the connection identifier,
If the connection identifier is the default value, the default value is the last opened connection.

Return Value: a result identifier is returned after the request is successful. If the request fails, false is returned.

$ SQL = "SELECT * FROM test ";
$ Result = @ mysql_query ($ SQL, $ conn) or die (mysql_error ());

3. Differences between the two query functions array/row

Format: mysql_fetch_row (result );

Note: mysql_fetch_row is used to save a row of query results to an array. The subscript of this array starts from 0, and each array element corresponds
One domain. You can obtain all query results through a loop.

Format: mysql_fetch_array (result );

Note: The functions of mysql_fetch_array and mysql_fetch_row are basically the same, except that they can be offset from 0.
You can also use a domain name as an index.

Returns all the domain values of the next row and saves them to an array. If no row exists, false is returned.

Mysql_query ("set names 'gbk'"); solves Chinese garbled characters

4. Introduction to other common Mysql Functions

Mysql_num_rows is used to calculate the number of rows in the query results.

Mysql_insert_id returns the ID of the last INSERT command

Mysql_tablename get the Database Name

Error message returned by mysql_error

Mysql_close close the MySQL Link

========================================================== ======================================

<? Php
$ Conn = @ mysql_connect ("127.0.0.1", "root", "root") or die ("database connection failed! ");
Mysql_select_db ("test", $ conn );
$ SQL = "select * from Admin ";
$ Result = mysql_query ($ SQL, $ conn) or die ("query error ");
// $ Row = mysql_fetch_row ($ result );
// Print_r ($ row );
// Echo "username:". $ row [1]. "Password:". $ row [2];
While ($ arr = mysql_fetch_array ($ result ))
{
Echo "username:". $ arr [1]. "Password:". $ arr [2];
Echo "<br> ";
}
?>

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.