Several common database operation functions in MySQL

Source: Internet
Author: User
Tags bool resource mysql database

Some commonly used MySQL operation functions, and the MySQL operation is closely related, I believe that the development of PHP's primary enthusiasts will be a great help.

1.mysql_connect--open a link to the MySQL server

Description: The link is closed after the page execution completes.

Example:

$link = mysql_connect ("localhost", "root", "") or Die ("Could not connect:". Mysql_error ());

2.mysql_close--close MySQL Link

function form: bool Mysql_close ([Resource link_identifier])

Description: Link_identifier closes the last open Non-persistent link by default.

Example:

$link = Myql_connect ("localhost", "root", "") or Die ("Link failed! Error message:". Mysql_error ());

echo "link Success";

Mysql_close ($link);?

3.mysql_pconnect--open an execution link to the MySQL server

Description: Similar to the mysql_connect, only established is a persistent link, more commonly used and convenient.

Example:

$link = Mysql_pconnect ("localhost", "root", "") or Die ("Could not connect:". Mysql_error ());

4.mysql_select_db--Choose MySQL Database

function form: bool mysql_select_db (string Database_name,resource link_identifier)

Note: Once the link is successful, you should immediately use this function to specify a database for the link identity.

Example:

$link = Myql_connect ("localhost", "root", "") or Die ("Link failed! Error message:". Mysql_error ()); echo "Link succeeded";

mysql_select_db ("Example", $link) or die (' cannot use example library ');

mysql_query ("INSERT into users (name) VALUES (' John ')");

Mysql_close ($link);

5.mysql_query--send a MySQL statement

Description: This is the most commonly used function, almost all SQL statements and MySQL-specific commands are executed through it.

Function form: Resource mysql_query (String Query,resource link_identifier)

Example:

$result = mysql_query ("SELECT * from users");

6.mysql_num_rows--the number of rows in the result set

function form: int mysql_rows (resource result)

Note: Gets the number of record bars for the result set, only valid for the result set returned by the Select. If it is the result of Update/insert, choose to use the Mysql_affected_rows function to get the number of records affected.

Example:

$result = My sample Sql_query ("SELECT * from users", $link);

$num _rows = mysql_num_rows ($result);

echo "Found it". $num _rows. " Record n ";

7.mysql_affected_rows--the number of record rows affected by the previous MySQL operation

function form: int mysql_affected_rows (Resource link_identifier)

Note: This function should be executed immediately after executing the mysql_query function of the corresponding SQL

Example:

mysql_query ("DELETE from users WHERE ID < 10");

echo "This operation has been removed". Mysql_affected_rows (). Line Record ";

8.mysql_error--returns the text error message from the previous MySQL operation

function form: String mysql_error (Resource Link_identifier)

Description: It returns the text message for the last time MySQL error occurred

9.mysql_insert_id--gets the ID generated by the previous insert operation

function form: int mysql_insert_id (Resource link_identifier)

Description: The primary key value that MySQL automatically generates when it gets the most recent insert operation

Example:

mysql_query ("INSERT into users (name) values (' Kossu ')");

Echo ("ID of last insert operation". mysql_insert_id ());

10.mysql_fetch_array--takes one row from the result set as an associative array, or an array of numbers, or both

function form: Array mysql_fetch_array (Resource Result,int result_type)

Description: Takes a record from the result set and moves it down a cursor, which makes it possible to access the target data through the array.

Parameter description: Result: Results set by mysql_query function query.

Result_type: Can only be Mysql_assoc, Mysql_num, Mysql_both, the return array index form. Default is Mysql_both.

Example:

$result = mysql_query ("SELECT id,name from Users");

while ($row = Mysql_fectch_array ($result))

{

echo "id=". $row [0]. "; Name= ". $row [" name "]."

";

}

Referring to these functions, you can write a MySQL operation class based on PHP, which includes the functions of connecting to MySQL, closing links, deleting data, modifying and adding content.

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.