Write PHP Paging Code mysql_fetch_arry () just can't execute

Source: Internet
Author: User
PHP Paging MySQL

/*
* Created on 2013-7-30
*
* To change the template for this generated file go to
* Window-preferences-phpeclipse-php-code Templates
*/

$page = $_get[page];
if (!function_exists (Pageft)) {
function Pageft ($totle, $displaypg =20, $url = ") {
Define several global variables:
$page: current page number;
$firstcount: The starting item for the (database tutorial) query;
$pagenav: the page navigation bar code, inside the function does not output it;
$_server: Read this page url "$_server[" Request_uri "]" must.
Global $page, $firstcount, $pagenav, $_server;
$globals ["Displaypg"]= $displaypg;
if (! $page) $page = 1;
If you use the default, or null value, for the value of the page, the URL is assigned:
if (! $url) {$url =$_server["Request_uri"];}

URL Analysis:

$parse _url=parse_url ($url);
$url _query= $parse _url["Query"]; Query string to take out the URL separately
if ($url _query) {
$url _query=preg_replace ("/(^|&) page= $page/", "", $url _query);
Replace the query string for the processed URL with the query string for the original URL:
$url =str_replace ($parse _url["Query"], $url _query, $url);
Add page query information after URL, but to assign value:
if ($url _query) $url. = "&page"; else $url. = "page";
}else {
$url. = "? page";
}

Page Count:

$LASTPG =ceil ($totle/$DISPLAYPG); Last page, also the total number of pages
$page =min ($LASTPG, $page);
$PREPG = $page-1; Previous page
$NEXTPG = ($page = = $lastpg? 0: $page + 1); Next page
$firstcount = ($page-1) * $DISPLAYPG;

Start Page navigation bar code:

$pagenav = "Show ". ($totle? ($firstcount + 1): 0). "- ". Min ($firstcount + $DISPLAYPG, $totle)."Records, a total of $totle records ";
If only one page jumps out of the function:
if ($lastpg <=1) return false;
$pagenav. = "Home";
if ($PREPG)
$pagenav. = "front page";
else $pagenav. = "front page";
if ($NEXTPG)
$pagenav. = "Back Page";
else $pagenav. = "Back Page";
$pagenav. = "Last";

Drop-down Jump List, looping through all page numbers:
$pagenav. = "To \ n "; for ($i =1; $i <= $lastpg; $i + +) {if ($i = = $page) $pagenav. = "$i \ n "; else $pagenav. = "$i \ n "; } $pagenav. = "Page, total $lastpg page ";
}
}


Include ("conn.php");

$result =mysql_query ("SELECT * from ' Test ');
$total =mysql_num_rows ($result);
Call Pageft (), displaying 10 messages per page (this parameter can be omitted using the default 20 o'clock), using the page URL (default, so omitted).
Pageft ($total, 3);
Echo $pagenav;

$result = "SELECT * from ' Test ' limit $firstcount, $displaypg";
echo $result;

$query =mysql_query ($result);
while ($row =mysql_fetch_array ($query)) {

echo " ". $row [name]." | ". $row [Sex];

}


?>


Reply to discussion (solution)

May be a problem with SQL, it is recommended that you post an error message for analysis.
Echo Mysql_error (); can see what

It's so complicated ... It's OK to replace the $firstcount with the parameters after get the link.

The output SQL statement. Then copy the phpMyAdmin inside, and if there is an error, it will be reported directly.

echo $result;
There's no problem with this.
Description SQL Statement no problem.

This error means you didn't find anything.

If I find out, I won't tell you: The parameter you gave is not a resource but a Boolean value

Check SQL statements

SELECT * from ' test ' limit 0,
There is a problem here, the paging length value is not loaded in

$DISPLAYPG =20
It should be set as a global variable and declared before your function definition.

$DISPLAYPG =20
It should be set as a global variable and declared before your function definition.


For this DISPLAYPG variable i define it as a global variable before it is placed in function and it seems useless. I think there are two problems with this code, one is that the paging length value is not loaded, and the second is that the SQL statement did not execute successfully, I was just in touch with PHP soon, ask the great God detailed guidance.
______3q!

You have $globals in your function ["DISPLAYPG"]= $displaypg;
The intention is to save the number of rows per page for subsequent use
But you didn't notice that the variable names in PHP are case-sensitive.
$GLOBALS ["Displaypg"]= $displaypg;
is what you need.

There's another place.
$page = $_get[page];
should be for
$page = $_get[page];
Otherwise it will not jump page

You have $globals in your function ["DISPLAYPG"]= $displaypg;
The intention is to save the number of rows per page for subsequent use
But you didn't notice that the variable names in PHP are case-sensitive.
$GLOBALS ["Displaypg"]= $displaypg;
is what you need.

There's another place.
$page = $_get[page];
should be for
$page = $_get[page];
Otherwise it will not jump page


Thank you, that's all.

  • 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.