mysql| Page | data | Show build and generate test data (Mysql.sql):
CREATE TABLE Pagetest (
ID int unsigned NOT NULL primary key auto_increment,
Name text NOT NULL
);
Insert into Pagetest (name) values (' Hello, friend, this is the pagination test data! ');
Insert into Pagetest (name) VALUES (' 2nd ');
Insert into Pagetest (name) VALUES (' 3rd ');
Insert into Pagetest (name) VALUES (' 4th ');
Insert into Pagetest (name) VALUES (' 5th ');
Insert into Pagetest (name) VALUES (' 6th ');
Insert into Pagetest (name) VALUES (' 7th ');
Insert into Pagetest (name) VALUES (' 8th ');
Insert into Pagetest (name) VALUES (' 9th ');
Insert into Pagetest (name) VALUES (' 10th ');
Insert into Pagetest (name) VALUES (' 11th ');
Insert into Pagetest (name) VALUES (' 12th ');
Insert into Pagetest (name) VALUES (' 13th ');
Display program (MYSQL.PHP3):
?
$pagesize = 2;//Display 2 records per page
$server = Mysql_pconnect ("localhost", "Test", "test") or Die ("Cannot connect to the MySQL database server on the localhost host!") //The MySQL database on the localhost host is connected to the test account and test password
$db = mysql_select_db ("test") or Die ("Cannot connect to the test 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!") ");/release result resource
$pagecount = Bcdiv ($rowcount + $pagesize-1, $pagesize, 0);/figure out a total of a few pages
if (!isset ($pageno)) $pageno = 1;//when PageNo is not set, the default is to display page 1th
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 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 ' up one 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) {//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
?>
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.