The MySQL access function needs to have the appropriate permissions to run. The commonly used correlation 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 a colon and port number to the host name. All parameters are optional, which, by default, corresponds 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 closed automatically, or it can be closed prematurely with mysql_close.
(2) Boolean mysql_create_db (database name);
Create a database. Note You must open the connection with an account with the CREATE DATABASE permission.
(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 select, a result number is returned, otherwise the returned value can be ignored. If it fails, returns false:
(5) Array mysql_fetch_array (result number);
Takes the next line and returns an array. can be accessed with a numeric subscript (the first field is subscript 0), or it can be accessed with a string subscript (that is, using each field name). Returns False if the last line has been taken:
(6) Mysql_fetch_row (result number);
Returns a matrix representing all the fields of a row in the result set. Each call produces the next line until there is no line left to return false. Each domain value is indexed by a zero-based 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 ();
Queries the server for a list of databases. It returns a result pointer that can be used for the Mysql_fetch_row function and similar functions.
(mysql_list_tables) (database name);
Gets a result pointer to a list of form lists for the specified database. The result pointer can be used for any function that fetches rows from the result set.
(one) 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 necessary because all non-permanent links are automatically closed at the end of the script.
(Mysql_pconnect) (host, user name, password);
Similar to mysql_connect, but establishes a "permanent connection" that is never closed once established, even if the Mysql_close function or program is executed. The next time you try to establish a permanent connection, the system discovers that a permanent connection exists. The connection number is returned directly without re-creation.
Example of invoking a MySQL database and displaying pagination
< $pagesize = 5; Each page shows 5 records $host= "localhost"; $user = "user"; $password = "PSW"; $dbname = "book"; The name of the library table being queried;//Connect MySQL database mysql_connect ("$host", "$user", "$password") or Die ("Unable to connect to 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 that generates the number of query records $rst = mysql_query ($sql) or Die ("Cannot 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 ("cannot release the result resource! "); Releases the result resource $pagecount = Bcdiv ($rowcount + $pagesize-1, $pagesize, 0);//Calculates a total of several pages if (!isset ($pageno)) {$pageno = 1;// When PageNo is not set, the default is to display the 1th page}if ($pageno <1) {$pageno = 1;//If 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));//$php_ The self is converted to a string that can be used on the URL, so that you can process the Chinese directory or the Chinese file name if ($pageno >1) {//Display the Walks echo "<a href=" "on the previous page. $href. "? pageno=". ($pageno-1). "> Prev </a>";} Else{echo "Previous page ";} 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) {//Displays the walks of the next page echo "<a href=" ". $href. "? pageno=". ($pageno + 1). "> Next </a>";} 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";//RAW The SQL statement to query the data on this page $rst = mysql_query ($sql);//query This page data $num_fields = Mysql_num_fields ($rst);//Get the total number of fields $i = 0;while ($i < $num _ Fields) {//Get all field names $fields[$i] = Mysql_field_name ($rst, $i);//Get the first name of the I+1 field $i++;} echo "<table border=" 1 "cellspacing=" 0 "cellpadding=" 0 ">";//Start Output form 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)) {//Displays the data on this page echo "<tr>"; Reset ($fields); while (List (, $ Field_name) =each ($fields)) {//Displays the value of each field $field_value = $row [$field _name];
PHP database operations Common related functions