How to invoke three kinds of databases in PHP (1)

Source: Internet
Author: User
Tags array functions integer connect mysql php and mysql database port number
Data | Database MySQL is a small and smart database server software, for medium and small application system is very ideal. In addition to supporting standard ANSI SQL statements, and most importantly, it supports multiple platforms, and on Unix/linux systems, MySQL supports multithreading, which allows for fairly good performance. Like PHP and Apache, it belongs to open source software. Its official website is: http://www.mysql.com, which provides Windows,linux,unix version of the source code download.

Note that MySQL access functions need to have the appropriate permissions to run. The most common related functions are described below:

(1) Integer mysql_connect (host, username, password);

This function begins a connection to the MySQL database on the specified host. If the database is located on a different port, the host name is followed by a colon and a port number. All parameters are optional and, by default, correspond to the local host, the script name that the user is executing, and the null. The host can be an IP address or a domain name.

At the end of the script execution, the connection is automatically turned off, or it can be mysql_close prematurely.

(2) Boolean mysql_create_db (database name);

Create a database. Note You must open the connection with an account with the permission to create the database.

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

Select the default database.

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

queries the specified database. If the SQL statement is a SELECT, returns a result number, otherwise the returned value can be ignored. Returns False if it fails ...

(5) Array mysql_fetch_array (result number);

Remove the next row and return an array. Can be accessed with a digital subscript (the first field is subscript 0), or you can use a string subscript access (that is, use each field name). Returns False if the last row has been taken ...

(6) Mysql_fetch_row (result number);

Returns a matrix that represents all the fields in a row in the result set. Each invocation produces the next line, and returns false until no rows are left. Each field value is indexed by a zero-starting offset. This is the quickest way to get results from a query.

(7) Integer mysql_num_rows (result number);

Returns the number of rows in the 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 to the server. It returns a result pointer that can be used for mysql_fetch_row functions and similar functions.

(a) Mysql_list_tables (database name);

Gets a result pointer to the list of tables in the specified database. The result pointer can be used for any function that gets rows from the result set.

(a) Mysql_close (connection number);

Closes the connection to the database. The connection must be open by mysql_connect. The use of this function is not strictly required because all non-persistent links are automatically closed at the end of the script.

(a) Mysql_pconnect (host, username, password);

Is exactly like Mysql_connect, but establishes a "permanent connection" that is never closed once established, even when the Mysql_close function or program is executed. The next time you attempt to establish a permanent connection, the system discovers that a permanent connection already exists. The connection number is returned directly without being recreated.

Here is an example of calling the MySQL database and paging it.

?

$pagesize = 5; Display 5 records per page

$host = "localhost";

$user = "User";

$password = "PSW";

$dbname = "book"; The name of the library table queried;

Connecting 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";//SQL statement to generate query records

$rst = mysql_query ($sql) or Die ("Unable to execute SQL statement: $sql!") "); Number of query records

$row = mysql_fetch_array ($rst) or Die ("No More records!") "); /Take out a record

$rowcount = $row ["Total"];//out the number of records

Mysql_free_result ($rst) or Die ("Unable to release the result resource!") "); Releasing a result resource

$pagecount = Bcdiv ($rowcount + $pagesize-1, $pagesize, 0);/figure out a total of a few pages

if (!isset ($pageno)) {

$pageno = 1; The default is to display page 1th when no pageno is set

}

if ($pageno <1) {

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

}

if ($pageno > $pagecount) {

$pageno = $pagecount; If the 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))//Convert $php_self to a string that can be used on the URL, so that you can handle the Chinese directory or the Chinese file name

if ($pageno >1) {//Show walks on previous page

echo "<a href=" ". $href. "? pageno=". ($pageno-1). "" > Prev </a>;

}

else{

echo "Prev";

}

for ($i =1; $i < $pageno; $i + +) {

echo "<a href=" ". $href. "? pageno=". $i. ">". $i. "</a>";

}

Echo $pageno. " ";

for ($i + +; $i <= $pagecount; $i + +) {

echo "<a href=" ". $href. "? pageno=". $i. ">". $i. "</a>";

}

if ($pageno < $pagecount) {//walks on next page

echo "<a href=" ". $href. "? pageno=". ($pageno + 1). "" > next page </a>;

}

else{

echo "Next page";

}

$offset = ($pageno-1) * $pagesize//figure out where the first record of this page is in the entire table (first record is 0)

$sql = "SELECT * from Pagetest LIMIT $offset, $pagesize"//SQL statement to generate query data on this page

$rst = mysql_query ($sql);//query data on this page

$num _fields = Mysql_num_fields ($rst);//Get total number of fields

$i = 0;

while ($i < $num _fields) {//Get the name of all fields

$fields [$i] = Mysql_field_name ($rst, $i);//Get the first name of I+1 field

$i + +;

}

echo "<table border=" 1 "cellspacing=" 0 "cellpadding=" 0 ">";//Start Output table

echo "<tr>";

Reset ($fields);

while (list (, $field _name) =each ($fields)) {//Display field name

echo "<th> $field _name</th>";

}

echo "</tr>";

while ($row =mysql_fetch_array ($rst)) {//Display this page data

echo "<tr>";

Reset ($fields);

while (list (, $field _name) =each ($fields)) {//display the value of each field

$field _value = $row [$field _name];

if ($field _value== "") {

echo "<td> </td>";

}

else{

echo "<td> $field _value</td>";

}

}

echo "</tr>";

}

echo "</table>";//Table Output end

Mysql_free_result ($rst) or Die ("Unable to release the result resource!") ");/release result resource

}

else{

echo "There is currently no data in the table!" ";

}

Mysql_close ($server) or Die ("Unable to disconnect 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.