PHP methods for calling three databases

Source: Internet
Author: User
Tags mysql functions
MySQL is a small and flexible database server software. it is a fantasy for small and medium-sized exploitation systems. In addition to supporting the scale of ANSISQL statements, the most important thing is that it also supports multiple platforms, while on Unix/Linux systems, mySQL supports multi-thread MySQL running. it is a small and flexible database server software, and it is a fantasy for small and medium-sized exploitation systems. In addition to ansi SQL statements supporting the scale, MySQL also supports multiple platforms. on Unix/Linux systems, MySQL supports multithreading, in this way, we can obtain good performance. Like PHP and Apache, Apache is an open source software. Its official website is: http://www.mysql.com, the above supply Windows, Linux, Unix version of the source code download.

Note that MySQL functions require the appropriate permissions to run. Common coherent functions are described as follows:

(1) integer mysql_connect (host, user name, password );

This function starts a connection to the MySQL database on the specified host. If the database is on a different port, add the colon and port number after the host name. All parameters are optional. by default, the script name corresponding to the local host and being executed by the user is distinguished as null. The host can be an IP address or domain name.

When the script is executed, the connection is automatically closed, or you can use mysql_close to close the connection in advance.

(2) boolean mysql_create_db (database name );

Create a database. Note that you must use an account with the permission to create a database to open the connection.

(3) boolean mysql_select_db (database name, connection number );

Select the default database.

(4) integer mysql_query (SQL statement, connection number );

Queries a specified database. If the SQL statement is select, a result number is returned. Otherwise, the returned value can be ignored. If it fails, false is returned ..

(5) array mysql_fetch_array (result number );

Retrieve the next row and return an array. you can use the number subscript to visit (the first field is subscript 0), or you can use the string subscript to visit (that is, use each field name ). If the last row is obtained, false is returned ..

(6) mysql_fetch_row (result number );

Returns a matrix representing all fields in a row in the result set. Each call generates the next line, and false is returned until no rows are left. Each domain value is indexed by an offset from the beginning. This is the fastest way to get results from queries.

(7) integer mysql_num_rows (result number );

Number of rows in the returned result set

(8) integer mysql_num_fields (result number );

Returns the number of fields in the result set.

(9) integer mysql_list_dbs ();

Query the database list on the server. It returns a result pointer, which can be used in the mysql_fetch_row function and similar functions.

(10) mysql_list_tables (database name );

Returns a result pointer to the list of tables pointing to the specified database. The result pointer can be used to retrieve rows from the result set.

(11) mysql_close (connection number );

Close the connection to the database. The connection must be opened by mysql_connect. The application of this function is not strictly required because all non-permanent links are automatically closed at the end of the script.

(12) mysql_pconnect (host, user name, password );

Similar to mysql_connect, but a 'permanent connection' is established, the connection is never closed once it is established, even if the mysql_close function is applied or the program is completed. in the next attempt to establish a permanent connection, if the system has found a permanent connection, it will directly return the connection number without recreating it.

The following is an example of calling the MYSQL database and displaying it on pages.


$ Pagesize = 5; // 5 records are displayed on each page

$ Host = 'localhost ';

$ User = 'user ';

$ Password = 'psw ';

$ Dbname = 'book'; // name of the database and table to be queried;

// Connect to the MySQL database

Mysql_connect ('$ host',' $ user', '$ password') or die (' cannot connect to the MySQL database server! ');

$ Db = mysql_select_db ('$ dbname') or die (' cannot connect to the database! ');

$ SQL = 'SELECT count (*) as total from pagetest '; // the SQL statement for querying the number of records by nature

$ Rst = mysql_query ($ SQL) or die ('unable to fulfill the SQL statement: $ SQL! '); // Query the number of records

$ Row = mysql_fetch_array ($ rst) or die ('no more records! '); // Retrieve a record

$ Rowcount = $ row ['total']; // number of records retrieved

Mysql_free_result ($ rst) or die ('cannot release result resources! '); // Returns the result resource

$ Pagecount = bcdiv ($ rowcount $ pagesize-1, $ pagesize, 0); // calculates the total number of pages

If (! Isset ($ pageno )){

$ Pageno = 1; // when pageno is not set, page 1st is displayed by default.

}

If ($ pageno <1 ){

$ Pageno = 1; // If pageno is smaller than 1, set it to 1.

}

If ($ pageno> $ pagecount ){

$ Pageno = $ pagecount; // If pageno is larger than the total number of pages, set it to the last page.

}

If ($ pageno> 0 ){

$ Href = eregi_replace ('/', '/', urlencode ($ PHP_SELF); // converts $ PHP_SELF to a string that can be applied on a URL, in this way, you can process a Chinese directory or a Chinese file name.

If ($ pageno> 1) {// display the response of the previous page

Echo 'previous page ';

}

Else {

Echo 'previous page ';

}

For ($ I = 1; $ I <$ pageno; $ I ){

Echo ''. $ I .'';

}

Echo $ pageno .'';

For ($ I; $ I <= $ pagecount; $ I ){

Echo ''. $ I .'';

}

If ($ pageno <$ pagecount) {// display the next page

Echo 'next page ';

}

Else {

Echo 'next page ';

}

$ Offset = ($ pageno-1) * $ pagesize; // calculates the position of the first record on this page in all tables (the first record is 0)

$ SQL = 'select * from pagetest LIMIT $ offset, $ pagesize '; // SQL statement used to query data on this page

$ Rst = mysql_query ($ SQL); // query data on this page

$ Num_fields = mysql_num_fields ($ rst); // obtain the total number of fields

$ I = 0;

While ($ I <$ num_fields) {// obtain the names of all fields

$ Fields [$ I] = mysql_field_name ($ rst, $ I); // Obtain the name of the I 1 field

$ I;

}

Echo'









































'; // Start output tableEcho' ';Reset ($ fields );While (list (, $ field_name) = each ($ fields) {// display the field nameEcho' ';}Echo' ';While ($ row = mysql_fetch_array ($ rst) {// Display data on this pageEcho' ';Reset ($ fields );While (list (, $ field_name) = each ($ fields) {// display the value of each field$ Field_value = $ row [$ field_name];If ($ field_value = ''){Echo' ';}Else {Echo' ';}}Echo' ';}Echo'
$ Field_name
$ Field_value
'; // Table output ends

Mysql_free_result ($ rst) or die ('cannot release result resources! '); // Returns the result resource

}

Else {

Echo 'The table does not have any data currently! ';

}

Mysql_close ($ server) or die ('cannot be disconnected from the server! '); // Disconnect and release resources

?>

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.