MySQL is a small and exquisite database server software, which is very ideal for small and medium-sized application systems. In addition to supporting standard ANSI SQL statements, the most important thing is that it also supports multiple platforms. On Unix/Linux systems, MySQL supports multi-threaded operation, which can achieve very good performance. Like PHP and Apache, it is an open source software. Its official website is: http://www.mysql.com, which provides source code downloads for Windows, Linux, and Unix versions.
Note that MySQL access functions require corresponding permissions to run. Commonly used related functions are introduced as follows:
(1)integer mysql_connect(host, username, 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 the port number after the host name. All parameters are optional and by default correspond to the local host, the name of the script being executed by the user, and empty. The host can be an IP address or a domain name.
At the end of script execution, the connection is automatically closed, or it can be closed in advance using mysql_close.
(2)boolean mysql_create_db (database name);
Create a database. Note that the connection must be opened with an account with permission to create databases.
(3)boolean mysql_select_db (database name, connection number);
Select the default database.
(4)integer mysql_query (SQL statement, connection number);
Query the specified database. If the SQL statement is select, a result number is returned, otherwise the returned value can be ignored. If it fails, return false.
(5)array mysql_fetch_array (result number);
Fetch the next row and return an array. It can be accessed with numeric subscript (the first field is subscript 0), or with String subscript access (that is, using each field name). If the last row has been fetched, return false.
(6)mysql_fetch_row(result number);
Returns a matrix representing all fields of a row in the result set. Each call produces the next row, returning false until there are no rows left. Each field value is indexed by a zero-based offset. This is the fastest way to get results from a query.
(7)integer mysql_num_rows(result number);
Return 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 server for the database list. It returns a result pointer that can be used with the mysql_fetch_row function and similar functions.
(10)mysql_list_tables(database name);
Get a result pointer pointing to the form list of the specified database. This result pointer can be used by 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. The use of this function is not strictly necessary since all non-permanent links are automatically closed at the end of the script.
(12) mysql_pconnect (host, username, password);
is completely similar to mysql_connect, but establishes a "permanent connection". Once established, the connection will never be closed, even if the mysql_close function is used Or the program is not closed after execution. The next time you try to establish a permanent connection, if the system finds that a permanent connection already exists, it will directly return the connection number without re-creating it.
The following is an example of calling a MYSQL database and displaying it in pages.
$pagesize = 5; //Display 5 records per page
$host="localhost";
$user= "user";
$password="psw";
$dbname="book"; //The name of the database table being 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";//Generate the SQL statement to query the number of 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 ! "); /Retrieve one record
$rowcount = $row["total"];//Retrieve the number of records
mysql_free_result($rst) or die("Cannot release result resource! "); //Release result resources
$pagecount = bcdiv($rowcount+$pagesize-1,$pagesize,0);//Calculate how many pages there are in total
if(!isset( $pageno)) {
$pageno = 1; //When pageno is not set, the default is to display page 1
}
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));//Convert $PHP_SELF into a string that can be used on the URL, so that Chinese can be processed Directory or Chinese file name
if($pageno>1){//Display the link to 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 " ";
}
if($pageno<$pagecount){//Display the link to the next page
echo "Next page ";
}
else{
echo "next page";
}
$offset = ($pageno-1) * $pagesize;//Calculate this page The position of the first record in the entire table (the first record is 0)
$sql = "select * from pagetest LIMIT $offset,$pagesize";//Generate the SQL statement to query the data on this page
$rst = mysql_query($sql);//Query the data on this page
$num_fields = mysql_num_fields($rst);//Get the total number of fields
$i = 0;
while($i<$num_fields){//Get the names of all fields
$fields[$i] = mysql_field_name($rst,$i);//Get the first Name of i+1 fields
$i++;
}
echo "
$field_name | ";|
---|---|
"; | $field_value | ";