MySQL operations in PHP

Source: Internet
Author: User
Many databases are supported in php, but when combined with the best database mysql, IT and linux, apache, and php constitute a LAMP system.

Many databases are supported in php, but when combined with the best database mysql, IT and linux, apache, and php constitute a LAMP system.

Many databases are supported in php, but when combined with the best database mysql, IT and linux, apache, and php constitute a LAMP system.

Php performs the following operations on mysql: connect to mysql database --> select database --> Execute SQL statements --> close result set --> close mysql database.

1. Connect to the database: mysql_connect ('hostname', 'username', 'Password ');

Lostname: host name. The default port is 3306.

Username: name of the database server user

Password: password corresponding to the user name

If the connection succeeds, a connection identifier is returned. Otherwise, false is returned.

$ Conn = mysql_connect ('localhost', 'root', '123 ');

If the @ symbol is added before mysql_connect (), the error message is not displayed.

2. Select the database mysql_select_db ('database name', 'Connection sign ')

$ Select = mysql_select_db ('db _ test', $ conn );

3. Execute the mysq statement, mysql_query ('Statement to be executed ', 'Connection sign ');

$ Result = mysql_query ("insert into db_user values ('qq', '11 ')");

4. retrieve the data in the result set mysql_fetch_array ($ reault );

Mysql_fetch_row ($ result );

The two differences are that the array obtained by the mysql_fetch_array function can enable the array of the numeric index to be an associated array. The array obtained by the mysql_fetch_row function can only be a numeric index.

$ Num = mysql_num_rows ($ result); this function returns the number of rows in the retrieved $ result data;

5. Release the memory mysql_free_result ($ result)

This function is used to release memory. After the database operation is complete, the result set needs to be closed to release the occupied resources. This function is used when the returned $ result set is large, because the system automatically releases the result set after the script execution ends.

6. Close mysql_close ()

The connection to the database in php is non-persistent, and the system generally does not need to close the connection because the system will automatically reclaim it. However, if the returned result set $ result is large or the site traffic is large, you need to disable the connection after use.

7. add, delete, modify, and query Databases

The addition, deletion, modification, and query operations on databases are mainly implemented using SQL commands, including the mysql_query () function and commands such as insert, update, select, and delect.

Generally, database operations are constantly performed in a system. Therefore, you need to write a piece of code to connect to the database in each database file, this makes code redundancy not easy to manage.

Generally, you can write a separate PHP file for database connection operations, and then use include_once () to call the file to connect to the database.

The world makes way for the man who knows where he is going.

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.