13-1 using mysqli in PHP to interact with MySQL

Source: Internet
Author: User
Tags mixed

1. Establish and close the connection to the MySQL server
1) Connect the specified MySQL server
[Email Protected]_connect ($host, $user, $password, $database, $port);
2) Tips for connecting errors
int Mysqli_connect_errno ();//Returns the error code of the last connection call
String Mysqli_connect_error ();//Returns a string describing the error code of the last connection call
3) Set the default character encoding
BOOL Mysqli_set_charset (mysqli $link, String $charset)
4) Select a specific database
BOOL mysqli_select_db (mysqli $link, string $dbname);
5) Close the connection to the MySQL server
BOOL Mysqli_close (mysqli $link);
2. SQL statement
1) Execute an SQL statement against the database
1>mixed mysqli_query (mysqli $link, String $query [, int $resultmode = Mysqli_store_result]);
① for SQL statements such as Insert,update,delete, which do not return data, returns True if no errors are executed.
② returns the result set object can use the function of the result set object to get the data from the successful execution of the SQL statement that returned the data
2) functions that manipulate result set objects
1> common functions for parsing data from a result set object
① retrieving data for a record in an indexed array
Mixed Mysqli_fetch_row (Mysqli_result $result);
Reuse to get data for the next record
② retrieving data for a record in an associative array
Array Mysqli_fetch_assoc (Mysqli_result $result);
Reuse to get data for the next record
③ gets the data for a record in an indexed array or an associative array
Mixed Mysqli_fetch_array (Mysqli_result $result [, int $resulttype = Mysqli_both]);
Reuse to get data for the next record
④ to get all the recorded data in an indexed array or an associative array
Mixed Mysqli_fetch_all (Mysqli_result $result [, int $resulttype = Mysqli_num]);
⑤ returns the next field information in the result set
Object Mysqli_fetch_field (Mysqli_result $result);
⑥ returns an array of objects representing the result set fields
Array Mysqli_fetch_fields (Mysqli_result $result);
⑦ gets the number of rows in the result
int mysqli_num_rows (Mysqli_result $result);
Note: If you use the Mysqli_use_result mode, you must finish getting the result set before you can use the function.
2> releasing memory associated with a result set
The returned result set requires a lot of memory support when the amount of data is large, so it is necessary to immediately release a result set associated with the result set.
Save, the result set is not available after release
void Mysqli_free_result (Mysqli_result $result);

<?PHPHeader(' Content-type:text/html;charset=utf-8 ');//establishing a connection to the MySQL database$link=Mysqli_connect(' localhost ', ' root ', ', ', 3306);//tips for connecting errorsif(Mysqli_connect_errno()){    Exit(Mysqli_connect_error());}//Set the default character encodingMysqli_set_charset ($link, ' UTF8 ');//Select a specific databasemysqli_select_db($link, ' D14 ');//execute a SQL statement against the database//$query = ' INSERT into T1 (info) VALUES ("Sun victory, Private library, sifangku.com");$query= ' SELECT * from T1 ';$result=Mysqli_query($link,$query);/*//Gets the data of a record in an indexed array while ($data =mysqli_fetch_row ($result)) {var_dump ($data);}*//*Gets the data var_dump (MYSQLI_FETCH_ASSOC ($result)) of a record in an associative array, Var_dump (MYSQLI_FETCH_ASSOC ($result)); Var_dump ( MYSQLI_FETCH_ASSOC ($result)); Var_dump (Mysqli_fetch_assoc ($result)); Var_dump (Mysqli_fetch_assoc ($result) );*//*gets the data for a record in an indexed array or an associative array the second parameter is optional: MYSQLI_ASSOC, Mysqli_num, Mysqli_both (default) Var_dump (Mysqli_fetch_array ($result, Mysqli_num));*//*get data for all records in an indexed array or associative array the second parameter is optional: MYSQLI_ASSOC, Mysqli_num, Mysqli_bothvar_dump (Mysqli_fetch_all ($result, mysqli _both));*//*returns the next field information in the result set $filed=mysqli_fetch_field ($result); Var_dump ($filed); Echo $filed->orgname;//reuse to get the next field Var_ Dump (Mysqli_fetch_field ($result)); Var_dump (Mysqli_fetch_field ($result));*//*returns an array of objects representing the result set fields Var_dump (Mysqli_fetch_fields ($result)); $fields =mysqli_fetch_fields ($result); echo $fields [0] ->name;*//*//Gets the number of rows in the result Var_dump (Mysqli_num_rows ($result));*///frees memory associated with a result setMysqli_free_result($result);//Close the connection to the MySQL serverMysqli_close($link);

13-1 using mysqli in PHP to interact with MySQL

Related Article

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.