Methods for PHP to call three databases (1)

Source: Internet
Author: User
Methods for PHP to call three databases (1) MySQL is a small and flexible database server software, which is ideal for medium and small application systems. In addition to standard ansi SQL statements, 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, provided above Windows, Linux, Unix version of the source code download.

Note: MySQL access functions must have the corresponding permissions to run. Common 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, they are corresponding to the local host, the script name being executed by the user, and null. The host can be an IP address or domain name.

When the script execution ends, 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. it can be accessed by the number subscript (the first field is subscript 0), or it can be accessed by the string subscript (that is, each field name is used ). 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 starting from scratch. This is the fastest way to get results from a query.

(7) integer mysql_num_rows (result number );

Number of rows in the returned result set

(8) integer mysql_num_fields (result number );

The number of fields in the returned 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. This result pointer can be used for any function that obtains rows from the result set.

(11) mysql_close (connection number );

Close the connection to the database. The connection must be opened by mysql_connect. This function is not strictly required because all non-permanent links are automatically disabled when the script ends.

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

Similar to mysql_connect, but a "permanent connection" is established. Once the connection is established, it will never be closed, even if mysql_close function or program execution is completed. if a permanent connection already exists in the system during the next attempt to establish a permanent connection, the connection number is directly returned without re-creation.

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 ("unable to connect to the MySQL database server! ");

$ Db = mysql_select_db ("$ dbname") or die ("unable to connect to the database! ");

$ SQL = "select count (*) as total from pagetest"; // an SQL statement that generates the number of query records

$ Rst = mysql_query ($ SQL) or die ("unable to execute 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 ("The result resource cannot be released! "); // Release the result resource

$ Pagecount = bcp ($ 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 ("% 2f", "/", urlencode ($ PHP_SELF); // converts $ PHP_SELF to a string that can be used 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 the entire table (the first record is 0)

$ SQL = "select * from pagetest LIMIT $ offset, $ pagesize"; // Generate an SQL statement 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 to output the 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 ("The result resource cannot be released! "); // Release the result resource

}

Else {

Echo "Currently, this table does not have any data! ";

}

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.