PHP mysql Extension: php access to MySQL's Common extension function

Source: Internet
Author: User
Tags field table php mysql php mysql extension

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

First, PHP connection database and basic operations

MySQL uses a ' client/server ' architecture. Using the MySQL extension function installed in PHP, and accessing the MySQL database server directly using the client software area, the principle is to send SQL commands to the MySQL management system and then return the results to the user.

In PHP, SQL is divided into two categories (see SQL Statement Classification): First, there are DQL statements that return the result set, such as the Select/desc table name, which requires PHP to process the result set after execution, and two that have no result set, such as DML, DDL, etc. However, the execution of the DML statement has an impact on the record of the data table.

<?php//connection database, common parameters are hostname, user name and password$link = mysql_connect (' localhost ',' Root ',' 123456 ');//Determine if the connection is successfulif(! $link) { die(' Connection failed '. Mysql.error ());//Connection successfully returned resource identifier, failed return false,mysql_error display error message}//Select a database, Mysql_error () is only used in debugging, no more when you deploy the project, or it will leak database informationmysql_select_db (' Test ')or  die(' Select database Failed '. Mysql_error ());//mysql_query () to set character sets and Execute SQL statementsmysql_query (' Set names Utf-8 '); $sql =' INSERT INTO Test (Id,name) VALUES ("1", "Dwqs") '; $result = mysql_query ($sql);//Execute SQL return result set//processing result set, insert belongs to DML, has an impact on table recordsif($result && mysql_affected_rows () > 0) {//mysql_insert_id () returns the Auto_increment value of the last new recordEcho ' Insert data success '. mysql_insert_id ().' <br/> ';}Else{Echo ' Insert data failed, error number: '. Mysql_errno ().' error message: '. Mysql_error ().' <br/> ';}//Close connectionMysql_close ($link);? >

Second, PHP processing select query result set

Executing a SELECT statement in PHP Returns a result set that can be used to process individual fields

$result  = mysql_query (' select * from Test ');  Gets the number of record rows $rows = mysql_num_rows ($result); //Gets the number of fields, that is, the data column $cols = Mysql_num_fields ($result);

If you need access to the data in the result set, you can use one of the following four functions (both with the result set resource as parameters and automatically return to the next record, returning false at the end of the table)

1, Mysql_fetch_row (): This function returns a result record and saves it as a normal index data.

2, Mysql_fetch_assoc (): Get a row from the result set as the associated data save

3. Mysql_fetch_array (): Gets a row from the result set as an associative array, or as an array of numbers, or both. The returned data pattern can be specified using MYSQL_ASSOC (associative array form), Mysql_num (indexed array form), and Mysql_both as the second parameter.

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

Recommendation: No special requirements, do not use mysql_fetch_array (), you can use Mysql_fetch_row () or MYSQL_FETCH_ASSOC () to achieve the same function, and high efficiency.

There are also three common functions associated with the result set

5, Mysql_data_seek (int $num): Moves the pointer of the internal result, $num is the number of rows of the new result set pointer that you want to set.

6, Mysql_fetch_lengths (Resource $result ): Get the length of each output in the result set

7, Mysql_result (resource $result , int $row[,mixed $field] ): Returns the contents of a cell in the MySQL result set. The field parameter can be either the offset of the field or the field name, or the Field table Point field name (Tablename.fieldname). If the column is aliased (' select Foo as bar from ... '), the column name is replaced with an alias. calling mysql_result () cannot be mixed with other functions that process the result set.


Next article: How to use WordPress to create a custom registration form plugin


PHP mysql Extension: php access to MySQL's Common extension function

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.