PHP operation MySQL Some of the commonly used built-in functions

Source: Internet
Author: User

1. Use the database:

To connect to a database:

$con = mysql_connect ($hostname, $user, $pass);

The connection returned successfully with a connection identifier , otherwise false

Select database:

mysql_select_db (' info '); The return value is a Boolean type

set the database character set:

mysql_query (' Set names UTF8 ');

To break a database link :

mysql_close ($con);

returns the previous error message:

mysql_error ();

2. Execute the SQL statement:

$query = mysql_query ($sql);

(1) SQL statements for additions and deletions, insert, delete, update

mysql_query () return value is Boolean type

when an INSERT statement is executed, the self-increment ID can be obtained through PHP's mysql_insert_id () , which can be used to determine whether the insert was successful or used as the correlation ID for other database operations.

(2) When the SQL statement is a query, select

Returns the resource identifier (address ) of the result set when the query succeeds, otherwise false

3. Process the result set:

(1) mysql_fetch_row ($query);

Each time the mysql_fetch_row is executed, it takes a data from the resource, which is the result set, and returns it as an array, and this time returns an empty result when the last data is currently taken.

The returned array is a one-dimensional indexed array , with each subscript corresponding to the sorting of fields in the database.

(2) Mysql_fetch_assoc ($query)

Each time the MYSQL_FETCH_ASSOC is executed, it takes a data from the resource, which is the result set, and returns it as an array, and this time returns an empty result when the last data is currently taken.

The returned array is a one-dimensional associative array , with each key value corresponding to the field in the database.

(3) mysql_fetch_array ($query, [param2])

Optional parameter param2:mysql_row: equivalent to Mysql_fetch_row ($query);

MYSQL_ASSOC: Equivalent to MYSQL_FETCH_ASSOC ($query);

Mysql_both (default):

Mysql_fetch_array ($query) executes every time, from the resource is the result set of a data, in the form of an array returned, the current time has been taken to the last data, this time to return an empty result.

The returned array is a one- dimensional indexed array and one-dimensional associative array .

(4) mysql_fetch_object ($query)

each time, from the resource is the result set to take a piece of data, returned as an object , the property in the object is the field name, the value corresponds to the corresponding field value

$object = Mysql_fetch_object ($query);

Echo $object->name;

4. Other commonly used:

(1)  mysql_num_rows ($query); Gets the number of bars recorded in the result set.

Can be used to determine whether the result set is empty.

if (mysql_num_rows ($query) {

}

(2) mysql_result () //Gets the value of the specified field for the given row

Mysql_result ($query, 1, 1);//Get 2nd row 2nd Field

mysql_result ($query, 1, ' name ')//Get 2nd row name Field

(3) mysql_affected_rows (); Records the number of rows affected after the last execution statement (INSERT, delete, update)

$con =mysql_connect ($host, $user, $pass);

Mysql_affected_rows ($con);

PHP operation MySQL Some of the commonly used built-in functions

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.