Php_mysql Extending primary operations

Source: Internet
Author: User

The MySQL extension is a collection of functions that PHP uses to provide the interface that the MySQL server provides, encapsulating a series of operations on the MySQL database.

1. Open the extension in php.ini, find the extension_dir, open the expansion.

2. In Phpinfo, check to see if the MySQL extension has been successfully opened.


Basic operations

mysql_connect        //Access DatabaseMysql_set_charset//Set character setsmysql_select_db        //Select Database#connecting to a database#IP Address, port number, account, password#localhost is local, if the port number is 3306 can be omitted$link=mysql_connect(' localhost ', ' root ', ' 123456 ');Var_dump($link);#Set character setsMysql_set_charset (' UTF8 ',$link);#Select database, ' Test ' for database namemysql_select_db(' Test ',$link);

A value for the resource type is obtained after the database connection succeeds. We can also set character sets and select databases by executing SQL statements.

mysql_query  // This function is used to execute the SQL statement, the same syntax as the direct operation of the MySQL statement mysql_query ("Set names UTF8"); mysql_query (" use test");

Adding and removing changes operation
Add or delete the operation is done by the mysql_query function to execute the SQL statement, by an SQL statement to achieve operations, such as:

$add = ' INSERT into table name (field) VALUES ("content") '; $update = ' Update table name set [modify content] '; $del = ' Delete from table name [condition] '; $res mysql_query ($add); $res mysql_query ($update); $res mysql_query ($del);

How to write in SQL operations, what to write in PHP, mysql_query () is equivalent to executing the statement directly in MySQL.

Query operations

By executing the query SQL statement through the mysql_query function, we get a resource-based result set, but this result can not be directly used, we need the exact data.

We need to iterate through the result set by MYSQL_FETCH_ASSOC the number of letters, to get the data we want.

Mysql_fetch_assoc

1. Executing the MYSQL_FETCH_ASSOC function will obtain an associative array, which corresponds to a record of the query;
2. Each execution of the MYSQL_FETCH_ASSOC function will get a new record, then the pointer moves down;
3. When you get to the last record, then execute the MYSQL_FETCH_ASSOC function, the pointer down will not be able to get the data, at this time return false;

According to the 2nd and 32 point characteristics, we can uniformly save the data into a new array variable through the while loop, such as:

#write a query statement, executed with Sql_query ()$sql= ' Select ' field from table name where [query condition] ';$result=mysql_query($sql);#set an empty array$rows=Array();#Mysql_fetch_assoc every time you get a record, put the results in the $row while($row=Mysql_fetch_assoc($result) ){    #use the while loop to put the results of each result into an array element    $rows[] =$row;}Var_dump($rows);

We can also use the Mysql_fetch_row function to parse the result set and get an indexed array (the following table)
By parsing the result set with the Mysql_fetch_array function, the resulting indexed array records and associative arrays contain

Description

1) Three functions can parse the result set and fetch a new record each time;

2) Three functions are the same from the effect, but in the returned result, the form of the element (key value or number of elements) is different

3) We have not only SELECT statement type query operation, we also have show, DESC and other query operations, can also be resolved by the result set to obtain the query data.

Related function functions in MySQL extension

mysql_field_name    // Gets the field name of the specified location field in the result set (subscript starting from 0) Mysql_num_fields    // get the number of fields in the result set (there are several fields that mean there are several columns) Mysql_errno      // returns the code name of the error message mysql_error      // returns the contents of the error message mysql_insert_id     // get the ID value of the most recently inserted data

Php_mysql Extending primary operations

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.