Basic Application of php statements in MySQL database

Source: Internet
Author: User

This article mainly introduces the basic application of php statements in MySQL databases. We all know that statements in MySQL databases are often used in practical applications, the following articles mainly introduce the specific application of MySQL database statements in php.

1. Use the mysql_connect () function to connect to the MySQL server: mysql_connect ("hostname", "username", "password ");

For example, $ link = mysql_connect ("localhost", "root", "911") or die ("cannot connect to the database server! The database server is not started, or the user name and password are incorrect! ". Mysql_error ());

2. use the mysql_select_db () function to select the database file: mysql_query ("use Database Name", $ link );

For example, $ db_selected = mysql_query ("use example", $ link );

3. Use the mysql_query () function to execute the SQL statement: mysql_query (string query (SQL statement), $ link );

For example:

Add a member: $ result = mysql_query ("insert into tb_member values ('A', '123')", $ link );

Modify Member: $ result = mysql_query ("update tb_member setuser = 'B', pwd = '000' where user = 'A'", $ link );

Delete Member: $ result = mysql_query ("delecte from tb_member where user = 'B'", $ link );

Query Member: $ SQL = mysql_query ("select * from tb_book ");

Fuzzy query: $ SQL = mysql_query ("select * from tb_book where bookname like '%". trim ($ txt_book). "% '");

// The common character % indicates zero or any number of characters.

Display table structure: $ result = mysql_query ("DESC tb_member ");

4. Use the mysql_fetch_array () function to obtain information from the array result set:

Syntax structure: array mysql_fetch_array (resource result [, int result_type])

The result parameter is a type parameter of the resource type. The integer parameter must be passed in the Data Pointer returned by the mysql_fetch_array () function;

Result_type: Optional. It is an integer parameter used to operate MySQL database statements in php. The input values are MYSQL_ASSOC (associated index) and MYSQL_NUM (Digital index) MYSQL_BOTH (including the first two and the default values)

For example:

 
 
  1. <1>$sql=mysql_query("select * from tb_book");  
  2. $info=mysql_fetch_object($sql);  
  3. <2>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");  
  4. $info=mysql_fetch_object($sql); 

5. Use the mysql_fetch_object () function to obtain a row from the result set as an object:

Syntax structure: object mysql_fetch_object (resource result );

For example:

 
 
  1. <1>$sql=mysql_query("select * from tb_book");  
  2. $info=mysql_fetch_object($sql);  
  3. <2>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");   
  4. $info=mysql_fetch_object($sql); 

The mysql_fetch_object () function is similar to the mysql_fetch_array () function. Only one difference is that an object instead of an array is returned. This function can only access the array by field name. Syntax structure of the row Element in the access result set: $ row-> col_name (column name)

6. Use the mysql_fetch_row () function to obtain each record in the result set row by row:

Syntax structure: array mysql_fetch_row (resource result)

For example:

 
 
  1. <1>$sql=mysql_query("select * from tb_book");  
  2. $row=mysql_fetch_row($sql);  
  3. <2>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");   
  4. $row=mysql_fetch_row($sql); 

7. Use the mysql_num_rows () function to obtain the number of concentrated records in the result:

Syntax structure: int mysql_num_rows (resource result)

For example:

 
 
  1. $sql=mysql_query("select * from tb_book");  
  2. ......  
  3. <?php $nums=mysql_num_rows($sql);echo $nums;?> 

Note: to obtain the data affected by the insert, update, and delete statements, you must use the mysql_affected_rows () function.

8. mysql_query ("set names gb2312"); // set the encoding format of MySQL to gb2312 to avoid garbled characters.

9. Disable record set: mysql_free_result ($ SQL );

10. Close the MySQL database server: mysql_close ($ conn );

The above content is a basic introduction to the php operation of MySQL database statements. I hope you will get some benefits.

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.