MySQL database functions (1)

Source: Internet
Author: User

1) int mysql_affected_rows ([int link_id]);

In a given connection, the number of rows affected by the last DELETE, INSERT, REPLACE, or UPDATE statement is returned. If no row is modified, mysql_affected_rows () returns 0. If an error occurs,-1 is returned.
After the SELECT query, mysql_affected_rows () returns the selected number of rows. However, it is generally used together with the SELECT statement.

Mysql_num_rows ().

Example:
<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Mysql_select_db ("samp_db") or die ("cocould not select database ");


$ Query = "INSERT

Member (last_name, first_name, expiration) "." VALUES ('brown ', 'marcia', '2017-6-3 ')";


$ Result = mysql_query ($ query) or die ("Query failed ");

Printf ("% d row % s insertedn", mysql_affected_rows (), mysql_affected_rows () = 1? "
":" S ");

?>

2) int mysql_close (int [link_id]);

Close the connection to the MySQL server identified by link_id. If no connection is specified, mysql_close () closes the recently opened connection. If the call succeeds, mysql_close () returns true, and false if the call fails. For a permanent connection opened by mysql_pconnect (), mysql_close () ignores the corresponding close request, but only returns the value. To close a connection, use mysql_connect () instead of mysql_pconnect.

Example:

<? Php

$ Link = mysql_connect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Print ("Connected successfully ");

Mysql_close ($ link );

?>

<EPRO_SPLIT>

3) int mysql_connect (string [hostname] [: port], string [username], string [password]);


This function establishes a connection with the MySQL server. All parameters can be omitted. When you use this function without adding any parameters, the hostname parameter's internal value is localhost, the username parameter's internal value is the PHP Execution itinerary owner, and the parameter password is a null string (that is, no password ). The hostname parameter can be followed by a colon and port number, indicating that the port is used to connect to MySQL. Of course, when using the database, you can use mysql_close () to turn off the connection earlier to save resources.

Example

This is an example provided by an anonymous user (18-Feb-1999)

<? Php

$ Dbh = mysql_connect ('localhost: 808080', 'mcclain', 'standard ');

Mysql_select_db ('admreqs ');

$ Query = "insert into requests (date, request, email, priority, status) values

(NOW (), '$ description',' $ email ',' $ priority ', 'new ')";

$ Res = mysql_query ($ query, $ dbh );

$ Query = "select max (id) from requests ";

$ Res = mysql_query ($ query, $ dbh );

$ Err = mysql_error ();

If ($ err ){

Echo "An error occurred, please notify <a href = mailto: webmaster@my.site> webmaster </a> ";


}

$ Row = mysql_fetch_row ($ res );

Echo "your future number is:". $ row [0];

?>


4) int mysql_create_db (string db_name [, int link_id]);

Tells the MySQL server identified by link_id to create a database with the given name. True is returned if the database is created successfully. False is returned if an error occurs. You must have the CREATE permission to CREATE a database. The use of mysql_query () may be more suitable than the use of mysql_create_db () to publish the create database statement.

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ");
Or die ("cocould not connect ");

If (mysql_create_db ("my_db "))

Print ("Database created successfullyn ");

Else

Print ("Error creating database: % sn", mysql_error ());

?>

(5) int mysql_data_seek (int result_id, int row_num );

Each result set returned by the SELECT query has a row cursor, indicating the row to be returned by the next function (mysql_fetch_array (), mysql_fetch_object (), or mysql_fetch_row () to extract the row. Mysql_data_seek () sets the pointer of the given result set to the given row. The row number ranges from 0 to mysql_num_rows ()-1. If the row number is valid, mysql_data_seek () returns true; otherwise, false.

<? Php

$ Link = mysql_pconnect ("localhost", "sunsoft", "suixiang ")
Or die ("cocould not connect ");

Mysql_select_db ("samp_db") or die ("cocould not select database ");


$ Query = "SELECT last_name, first_name FROM president ");

$ Result = mysql_query ($ query) or die ("Query failed ");

For ($ I = mysql_num_rows ($ result)-1; $ I> = 0; $ I -)

{

If (! Mysql_data_seek ($ result, $ I );

{

Printf ("Cannot seek to row % dn", $ I );

Continue;

}

If (! $ Row = mysql_fetch_object ($ result )))

Continue;

Printf ("% d % s <BR> n", $ row-> last_name, $ row-> first_name );

}

Mysql_free_result ($ result );

?>


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.