PHP operation MySQL

Source: Internet
Author: User

One, Tianlong eight steps

1. Connect to MySQL Database

1 $link=mysqli_connect("localhost", "root", "Meiyoumima");

2. Determine if the connection is successful

1 if ($link) {23echo "database connection succeeded! "; 4 }else{56die ("Database connection Failed"); 7 }              

3. Setting the character set

1 mysqli_set_charset ($link, "UTF8");

4. Select a database

1 mysqli_select_db ($link, "demo");

5. Preparing SQL statements

1 $sql= "SELECT * from Stu";

6. Send SQL statements to the MySQL service

1 $result=mysqli_query($link,$sql);  

7. Parse the result set

1 Echo"<center>";2     Echo";3     Echo"<table border= ' 1 ' width= ' >";4         Echo"<tr>";5             Echo"<th>ID</th>";6             Echo"<th> name </th>";7             Echo"<th> Gender </th>";8             Echo"<th> Age </th>";9             Echo"<th> class </th>";Ten             Echo"<th> Operations </th>"; One         Echo"</tr>"; A          -     //5. Parse the result set -     if($result&&mysqli_num_rows($result) >0){ the          while($rows=Mysqli_fetch_assoc($result)){ -             Echo"<tr align= ' center ' >"; -                 Echo"<td>{$rows[' ID ']} </td> "; -                 Echo"<td>{$rows[' Name ']} </td> "; +                 Echo"<td>{$rows[' Sex ']} </td> "; -                 Echo"<td>{$rows[' Age ']} </td> "; +                 Echo"<td>{$rows[' ClassName ']} </td> "; A                 Echo"<td> at <a href= ' # ' > Edit </a> - <a href= ' # ' > Delete </a> -</td> "; -             Echo"</tr>"; -         } -}Else{ in          die("No data is queried!") "); -     } to     Echo"</table>"; +     Echo"</center>";

8. Release the result set and close the database connection

1 Mysqli_free_result ($result); 2 Mysqli_close ($link);      

Two or six-pulse Excalibur

1     //1. Connect to the database server (if the connection succeeds as an object, if it fails, it returns a false)2     $link=Mysqli_connect("localhost", "root", "Meiyoumima") or die("Database connection failed! ");3     4     //2. Setting the Encoding5Mysqli_set_charset ($link, "UTF8");6     7     //3. Select a database8     mysqli_select_db($link, "Demo");9     Ten     //4. Define the SQL statement and send execution (if the send execution succeeds, is an object, if it fails, is a false) One     $sql= "SELECT * from Stu;"; A     $result=Mysqli_query($link,$sql); -      -     Echo"<center>"; the     Echo"; -     Echo"<table border= ' 1 ' width= ' >"; -         Echo"<tr>"; -             Echo"<th>ID</th>"; +             Echo"<th> name </th>"; -             Echo"<th> Gender </th>"; +             Echo"<th> Age </th>"; A             Echo"<th> class </th>"; at             Echo"<th> Operations </th>"; -         Echo"</tr>"; -          -     //5. Parse the result set -     if($result&&mysqli_num_rows($result) >0){ -          while($rows=Mysqli_fetch_assoc($result)){ in             Echo"<tr align= ' center ' >"; -                 Echo"<td>{$rows[' ID ']} </td> "; to                 Echo"<td>{$rows[' Name ']} </td> "; +                 Echo"<td>{$rows[' Sex ']} </td> "; -                 Echo"<td>{$rows[' Age ']} </td> "; the                 Echo"<td>{$rows[' ClassName ']} </td> "; *                 Echo"<td> $ <a href= ' # ' > Edit </a>Panax Notoginseng <a href= ' # ' > Delete </a> -</td> "; the             Echo"</tr>"; +         } A}Else{ the          die("No data is queried!") "); +     } -     Echo"</table>"; $     Echo"</center>"; $     //6. Close the database and release the result set -     Mysqli_free_result($result); -     Mysqli_close($link);

Three, common function 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 (); ---parsing a result set in both associative and indexed arrays 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); --Gets the number of data bars in the result set (specifically for queries)

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

Mysqli_result (); --Positioning results
Echo Mysqli_result ($result, 0, 3). " <br/> "; Gets the value in the 4th column of the 1th piece of data
Echo Mysqli_result ($result,.). " <br/> "; Gets the value in the 3rd column of the 2nd piece of data
Echo Mysqli_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 Insert,update or DELETE query associated with the number of record rows affected by the previous MySQL operation.

Mysqli_insert_id-get the ID from the previous insert operation

PHP operation MySQL

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.