Home  >  Article  >  Backend Development  >  Some commonly used built-in functions for php to operate mysql database

Some commonly used built-in functions for php to operate mysql database

怪我咯
怪我咯Original
2017-06-28 10:08:081877browse

1. Use the database:

Connect to the database:

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

If the connection is successful, a connection identifier will be returned, 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');

Disconnect database link

mysql_close($con);

##Return the previous error message:

##mysql_error() ;2. Execute sql statement:

$query = mysql_query($sql);

(1) sql The statements are add, delete, update

The return value of mysql_query() is a Boolean type

After executing the insert statement, you can use php's

mysql_insert_id()To get the auto-incremented id, this id can be used to determine whether the insertion is successful, or used as an associated id for other database operations.

(2) When the sql statement is a query, select When the query is successful, the

resource identifier (address

of the result set is returned) ), otherwise return false## 3. Process the result set:

(1)

mysql_fetch_row($query )

;##                                                                                                                                                                    Every time mysql_fetch_row is executed, a piece of data is fetched from the resource, that is, the result set, and returned in the form of an array. The last piece of data has been fetched at this time. , this time an empty result is returned.

The returned array is a one-dimensional index array

, each subscript corresponds to the sorting of the fields in the database.

(2)

mysql_fetch_assoc

($query) mysql_fetch_assoc is executed every time Each time, a piece of data is fetched from the resource, that is, the result set, and returned in the form of an array. When the last piece of data has been fetched the previous time, an empty result is returned this time.

The returned array is a one-dimensional associative array, each key value corresponds to a 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):

This is performed by the result of the result of the result of the result of the resource. Fetch a piece of data one by one and return it in the form of an array. When the last piece of data has been fetched previously, an empty result is returned this time.

The array returned is

an one -dimensional index array and a one -dimensional associated array

.

(4)

mysql_fetch_object($query)

          Each time it is executed, a piece of data is taken from the resource, that is, the result set, and returned in the form of object. The attributes in the object are the field names, and the values ​​correspond to Corresponding field value

## $object = mysql_fetch_object($query);

echo $object->name;

##4. Other commonly used ones:

(1)mysql_num_rows($query); Get the number of records in the result set.

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

# if(mysql_num_rows($query){

      }

## (2)

mysql_result()

//Get the specified field of the specified row The value of ## mysql_result($query,1,1);//Get the second field of row 2

## mysql_result($query,1,'name') //Get the name of row 2

Field## (3)mysql_affected_rows();

//Record the number of affected rows after the last executed statement (insert, delete, update)

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

## mysql_affected_rows($con);

The above is the detailed content of Some commonly used built-in functions for php to operate mysql database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn