MySQL extension in PHP: A common extension function used by PHP to access MySQL.

Source: Internet
Author: User
Tags field table php database

MySQL extension in PHP: A common extension function used by PHP to access MySQL.

Source: http://www.ido321.com/1024.html

I. PHP database connection and basic operations

MySQL adopts the 'client/Server' architecture. Using the MySQL extension function installed in PHP is the same as directly accessing the MySQL database server using the client software zone. In the same principle, you must send an SQL command to the MySQL management system and then return the result to the user.

In PHP, SQL is classified into two types (view SQL statements): one is a DQL statement with a returned result set, such as the select/desc table name. After execution, PHP is required to process the result set. Second, there is no result set, such as DML and DDL,However, after the DML statement is successfully executed, the data table records are affected.

<? Php // connect to the database. Common parameters include host name, user name, and password $ link = mysql_connect ('localhost', 'root', '123 '); // determine whether the connection is successful if (! $ Link) {die ('Connection failed '. mysql. error (); // Resource Identifier returned for successful connection, false returned for failed, error message displayed for mysql_error} // select database, and mysql_error () is only used in debugging, do not deploy the project again. Otherwise, the database information will be leaked. mysql_select_db ('test') or die ('database selection failed '. mysql_error (); // mysql_query () can set character sets and execute SQL statements mysql_query ('set names UTF-8 '); $ SQL = 'insert into test (id, name) values ("1", "dwqs") '; $ result = mysql_query ($ SQL); // Execute SQL to return the result set // process the result set, insert belongs to DML, if ($ result & mysql_affected_rows ( )> 0) {// mysql_insert_id () returns the auto_increment value of the last new record echo 'data inserted successfully '. mysql_insert_id (). '<br/>';} else {echo 'failed to insert data, error code :'. mysql_errno (). 'error message :'. mysql_error (). '<br/>';} // close the connection mysql_close ($ link);?>

 

Ii. PHP processes the select query result set

Execute the select statement in PHP to return a result set, which can be used to process each field

$ Result = mysql_query ('select * from test'); // obtain the number of record rows $ rows = mysql_num_rows ($ result); // obtain the number of fields, that is, the data column $ cols = mysql_num_fields ($ result );

If you need to access data in the result set, you can use one of the following four functions (the resource operator of the result set is used as a parameter, and the next record is automatically returned, and false is returned at the end of the table)

1. mysql_fetch_row (): This function returns a result record and stores it with a common index data.

2. mysql_fetch_assoc (): obtain a row from the result set and save the row as the associated data.

3. mysql_fetch_array (): Get a row from the result set as an associated array, or a number array, or both. You can use MYSQL_ASSOC (associated array form), MYSQL_NUM (Index Array form), and MYSQL_BOTH as the second parameter to specify the returned data form.

4. mysql_fetch_object (): obtains a row from the result set as an object, and each field is accessed as an object.

Suggestion: there are no special requirements. Do not use mysql_fetch_array (). You can use mysql_fetch_row () or mysql_fetch_assoc () to implement the same function and improve efficiency.

There are also three common functions related to the result set.

5. mysql_data_seek (int $ num): Move the internal result pointer. $ num is the number of rows of the new result set pointer to be set.

6. mysql_fetch_lengths (resource$result): Gets the length of each output in the result set.

7. mysql_result (resource$result, Int$row[,mixed $field]): Returns the content of a unit in the MySQL result set. The field parameter can be the offset or field name of the field, or tablename. fieldname of the field table ). If an alias ('select foo as bar from... '), The column name is replaced by an alias. CallMysql_result ()It cannot be called together with functions in other processing result sets.

 

Next article: How to Use WordPress to create a custom registry single plug-in


 


In phpmyadmin, make sure that PHP enables the Mysql extension library. What is it?

Make sure that PHP enables the Mysql extension library, which means:
Modify the php. ini file and you will find that there are a lot of prefixed #
This is a comment.
What you need to do is
① Open php. ini
② Search for extension = php_mysql.dll
③ Remove the # above this sentence

By now, PHP has enabled the Mysql extension library.
 
How to load MYSQL extension in PHP 5213

Modify the php. ini file in two places:
1. Remove the semicolon before extension = php_mysql.dll;
2. Modify extension_dir = "php installation directory/ext ";
Finally, restart the server.

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.