Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Extended mysql function handout

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following mainly brings you the handout of the extended mysql function. I hope these words can bring you practical use, which is also the main purpose of my editing this article. All right, don't talk too much nonsense, let's just read the following.

Because of its small size, high speed and low total cost of ownership, especially with open source, many small and medium-sized websites choose mysql as the website database in order to reduce the total cost of ownership. The database system solution using the combination of mysql database management system and php scripting language is being adopted by more and more websites, among which LAMP (linux+apche+mysql+php) mode is the most popular.

PHP has standard functions for manipulating databases. Mysqli is a new addition to PHP5 and is an improvement on the mysql extension. However, due to historical problems, many old projects are developed using mysql in PHP4. If the secondary development is carried out on the original project, it is required to use the mysql extension function. If it is a newly designed project, it is recommended to use mysqli extension or PDO technology. This paper mainly introduces the mysql extension function in PHP.

Summary

The steps to manipulate the MySQL database in a PHP script are as follows:

1. Connect to the MySQL database cloud server and determine whether the connection is correct.

2. Select the database and set the character set (optional)

3. Execute the SQL command

4. Process the result set

5. Close the database connection

Connect to the MySQL database cloud server and determine whether the connection is correct

Mysql_connect ()

The mysql_connect () function is used to open a connection to the MySQL server. Return a resource if successful, or FALSE on failure

Resource mysql_connect ([string $server [, string $username [, string $password [, bool $new_link [, int $client_flags])

Mysql_errno ()

The mysql_errno () function is used to return the numeric encoding of the error message in the previous MySQL operation

Int mysql_errno ([resource $link_identifier])

Mysql_error ()

The mysql_error () function is used to return a text error message from the previous MySQL operation. If the connection resource number is not specified, the error message is extracted from the MySQL server using the last successful connection

String mysql_error ([resource $link_identifier]) selects the database and sets the character set (optional)

In general, the creation of a database is first established by a database administrator (DBA) and then used by PHP programmers in scripts. For example, create a database called bookstore

After using the PHP script to establish a connection to the mysql server, in order to avoid specifying the target database every time you call the mysql extension function of PHP, it is best to use the mysql_select_db () function to select a default database for subsequent operations, which is similar to the SQL command "USE bookstore" function.

Mysql_select_db ()

The mysql_select_db () function is used to select a MySQL database

Bool mysql_select_db (string $database_name [, resource $link_identifier])

Execute the SQL command

First, create an books data table in the bookstore database

CREATE TABLE books (id INT NOT NULL AUTO_INCREMENT, bookname VARCHAR (80) NOT NULL DEFAULT'', publisher VARCHAR (60) NOT NULL DEFAULT'', author VARCHAR (20) NOT NULL DEFAULT'', price DOUBLE (5L2) NOT NULL DEFAULT 0.00, ptime INT NOT NULL DEFAULT 0, pic CHAR (24) NOT NULL DEFAULT', detail TEXT, PRIMARY KEY (id);)

In PHP, as long as the SQL command is passed to the mysql_query () function as a string, it is sent to the MYSQL server and executed

Mysql_query ()

The mysql_query () function is used to send a MySQL query. Mysql_query () returns only one resource for SELECT,SHOW,DESCRIBE, EXPLAIN and other statements, and returns FALSE; if there is an error in the query. For other types of SQL statements, such as INSERT, UPDATE, DELETE, DROP, etc., mysql_query () returns TRUE on success and FALSE on error.

Resource mysql_query (string $query [, resource $link_identifier = NULL])

The three INSERT statements to be inserted are declared as a string

$insert = "insert into books (bookname, publisher, author, price, detail) values ('PHP',' Electronic Industry Publishing House', 'Zhang San', '80.00Ji'), ('ASP',' Electronic Industry Publishing House','Li Si', '90.00Zhi Zhe JPSPrelated'), ('JSP',' Electronic Industry Publishing House', 'Wang Wu', '70.00Zhi Jian Jian JSPP related')"

Use the mysql_query () function to send an INSERT statement. If true is returned successfully, false will be returned if it fails.

$result = mysql_query ($insert); var_dump ($result)

Mysql_affected_rows ()

The mysql_affected_rows () function is used to get the number of rows of records affected by the previous MySQL operation. If the execution is successful, it returns the number of rows affected. If the last query fails, the function returns-1.

Int mysql_affected_rows ([resource $link_identifier = NULL]) var_dump (mysql_affected_rows ()); / / int3

The success of the data operation is usually determined by determining whether the value of the mysql_affected_rows () function is greater than 0.

Mysql_insert_id ()

The mysql_insert_id () function is used to get the ID generated by the previous INSERT operation.

Int mysql_insert_id ([resource $link_identifier])

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report