"PHP" PHP operation MySQL Database

Source: Internet
Author: User

First, the operation steps:
1. Connect to MySQL database and determine if the connection is successful
2. Select a database
3. Setting the character set
4. Preparing SQL statements
5. Send SQL statements to the MySQL service
6. Parse the result set
7. Release the result set and close the database connection

Second, common operation:
1. Mysqli_connect ();--Connect to the database and return a connection resource
Format: mysql_connect (host name, user, password);
--where parameters are optional, if not written, refer to the default configuration in php.ini

2. Mysqli_error (); --Get the error message just (last) to perform the database operation

3. Mysqli_errno (); --Get the error number that just (last) performed the database operation
Error number 0 means no error

4. mysqli_select_db (database name [, database connection]);
Select a database, equivalent to the Use library name statement

5. Mysqli_set_charset (character encoding);--Set character encoding
For example: Mysql_set_charset ("UTF8"); Equivalent to: mysql_query ("Set names UTF8");

6. Mysqli_query (SQL statement [, database connection]);--Send an SQL statement
If the SQL statement is queried, the result set is returned, and the other returns a Boolean value indicating whether the execution was successful.

7. Parse the result set function:
Mysqli_fetch_array (); --Parse the result set in an array of associative and indexed ways
You can also specify a second parameter to define the return format:
Mysql_both (0 associations and indexes)/mysql_num (2 index)/MYSQL_ASSOC (1 associations)

Mysqli_fetch_row (); --Parse the result set in an indexed array
*mysqli_fetch_assoc (); --parse result set with associative array
Mysqli_fetch_object (); --Parse the result set in object mode

8. Mysqli_free_result (Result set name);--release result set

9. Mysqli_close (database connection); --Close database connection

Mysqli_num_rows (result set); --Get the number of data bars in the result set

Mysqli_num_fields (result set); --Get the number of columns in the result set (number of fields)

Mysqli_result (); --Positioning results
Echo mysql_result ($result, 0, 3). " <br/> "; Gets the value in the 4th column of the 1th piece of data
Echo mysql_result ($result,.). " <br/> "; Gets the value in the 3rd column of the 2nd piece of data
Echo mysql_result ($result, 5,4). " <br/> "; Gets the value in the 5th column of the 6th piece of data

13.mysqli_affected_rows-gets the number of record rows affected by the previous MySQL operation
The number of record rows affected by the associated insert,update or DELETE query.

Mysqli_insert_id-get the ID from the previous insert operation

1 //Six-Pulse Excalibur in PHP that operates MySQL database2 3 //1. Connect to the database server and determine if the Mysqli_connect is successful (server address, user name, password [, database name]);4 $link=Mysqli_connect(' localhost ', ' root ', ');5 6 //2. Set the character set Mysqli_set_charset (Database server object, encoding type to be set);7Mysqli_set_charset ($link, ' UTF8 ');8 9 //3. Select Database mysqli_select_db (Database server object, specified database name in database server);Ten mysqli_select_db($link, ' lamp183 '); One  A //4. Input Add, delete, change, check SQL statement, and send execution Mysqli_query (database server object, SQL statement); - $sql= ' SELECT * from Stu '; - $result=Mysqli_query($link,$sql); the Var_dump($result); -  - //5. Parse the result set Mysqli_num_rows (query sends the object result set after the SQL statement), will get the total number of the query data; - if($result!=false&&mysqli_num_rows($result) >0){ +      -     //iterate over the array that gets each piece of data Mysqli_fetch_assoc (query sends an object result set after the SQL statement), and you get an array of each piece of data +      while($rows=Mysqli_fetch_assoc($result)){ A         Var_dump($rows); at     } - } -  - //6. Close the database and release the result set - Mysqli_free_result($result); - Mysqli_close($link);
1 //Six-vein Excalibur operation database2 3 //1. Connect to the database server and determine4 $link=Mysqli_connect(' localhost ', ' root ', ') or die(' Database connection failed! ‘);5 6 //2. Setting the character set7Mysqli_set_charset ($link, ' UTF8 ');8 9 //3. Select a databaseTen mysqli_select_db($link, ' lamp183 '); One  A //4. Define Add, delete, change SQL statements, and send execution -  - //Define SQL statement to add data (return Boolean true or false after send) the     $sql= "INSERT into Stu (name) VALUES (' Jingfei ')"; -     $bool=Mysqli_query($link,$sql); -      -     //Gets the ID number of the previous insert successful data +     $id=mysqli_insert_id($link); -      +     //define SQL statements to delete data A     $sql= "Delete from Stu where id=2"; at     $bool=Mysqli_query($link,$sql); -      -     //define SQL statements that modify data -     $sql= "Update stu set name= ' Haibo ' where name= ' Haibo '"; -     $bool=Mysqli_query($link,$sql); -      in //5. Determine whether to perform a successful Mysqli_affected_rows (object result set): Returns the number of affected rows after a successful SQL statement execution, if the failure returns 1 - if($bool!=false&&mysqli_affected_rows($link) >0){ to     Echo"<script>alert (' Congratulations, add success! '); window.location.href= ' 3.php </script> "; + } -  the //6. Close the database * Mysqli_close($link);

"PHP" PHP operation MySQL Database

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.