Integrated the PHP database connection class ~~ Create a paging class!

Source: Internet
Author: User
Tags php database php database connection class

I don't know if I want to learn PHP ~ The more you write, the less boring you are.

<? Php
Class createdb // Class start
{
Var $ db = "localhost"; // database address;
Var $ dbname = "root"; // user name;
Var $ dbpwd = ""; // password;
Var $ dbtable = "mysql"; // database used
Var $ conn; // database connection;
Var $ result; // result set
Var $ mysql; // The executed mysql
Var $ row; // find data in the result set
Function createconn () // This class method is to start a conn connection, and then start to select a database
{
$ This-> conn = mysql_connect ($ this-> db, $ this-> dbname, $ this-> dbpwd );
Mysql_select_db ($ this-> dbtable, $ this-> conn );
}
Function getresule () // This is a result set.
{
$ This-> result = mysql_query ($ this-> mysql, $ this-> conn );
}
Function getrow () // create a forward result set pointer
{
$ This-> row = mysql_fetch_array ($ this-> result );
Return $ this-> row;
}
Function closedatabase ()
{
Mysql_close ($ this-> conn );
}
} // When the class ends, the class is called to read mysql database data.
// Debug the database connection code! Not required.
/* $ Bb = new createdb; // class instantiation
$ Bb-> createconn (); // call the connection of the class
$ Bb-> mysql = "select * from friends_conf ";
$ Bb-> getresule (); // call the class to obtain the result set.
While ($ bb-> getrow () {// call the class to create a pointer to read data in a forward read loop,
Echo ($ bb-> row ["confname"]);
Echo ($ bb-> row ["confvalue"]);
}*/
?>
// Paging class ~ Inherits the above method
<? Php
Class pagefy extends createdb {
Var $ nametatol; // The total number of records
Var $ namemust = 1; // number of records to be displayed on each page
Var $ pageall;
Var $ I = 1;
Var $ m;
Var $ p = 8; // the page number to be displayed;
Var $;
Function gettatol () // Method for retrieving the total number of records
{// Connect to the database first
$ This-> nametatol = $ this-> row [0]; // The total number of records
}
Function getpage () // retrieve the total number of pages
{
$ This-> pageall = $ this-> nametatol % $ this-> namemust;
If ($ this-> pageall)
{
$ This-> pageall = (int) ($ this-> nametatol/$ this-> namemust) + 1;
Return $ this-> a = $ this-> pageall;
}
Else
{
Return $ this-> a = $ this-> pageall = (int) ($ this-> nametatol/$ this-> namemust );
}
}
Function num_pl () // page number display
{
$ This-> pageall = $ this-> pageall-($ this-> pageall-$ this-> p );
While ($ this-> pageall)
{
Echo ("<a href = 'pagefy. php? N = $ this-> I '> $ this-> I </a> ");
Echo (""); // you can add the desired style
$ This-> I ++;
$ This-> pageall = $ this-> pageall-1;
}
}
Function get_num_pl () // The previous parameter of the LIMIT (start ROW number)
{
$ This-> m = $ _ GET ["n"] * $ this-> namemust-$ this-> namemust;
}
Function showpage () // display the page content
{
$ This-> createconn ();
$ This-> mysql = "select * from friends_conf limit $ this-> m, $ this-> namemust ";
$ This-> getresule ();
While ($ this-> getrow ())
{
Echo ($ this-> row ["confname"]); // Add the field you want to read
Echo ($ this-> row ["confvalue"]);
}
}
Function firstpage () // Home Page
{
Echo ("<a href = 'pagefy. php? N = 1'> homepage <a> ");
}
Function backpage () // Previous Page
{
If ($ this-> I =$ _ GET ["n"]-1)
{
Echo ("<a href = 'pagefy. php? N = $ this-> I '> previous page <a> ");
}
}
Function nextpage () // next page
{
$ This-> I =$ _ GET ["n"] + 1;
If ($ this-> I <= $ this->)
{
Echo ("<a href = 'pagefy. php? N = $ this-> I '> next page <a> ");
}
}
Function footpage () // The last page
{
Echo ("<a href = 'pagefy. php? N = $ this-> a'> last page <a> ");
}
}
?>
<? Php // debugging
$ Mm = new pagefy;
$ Mm-> createconn ();
$ Mm-> mysql = "select count (*) from friends_conf ";
$ Mm-> getresule ();
$ Mm-> closedatabase ();
$ Mm-> getrow ();
$ Mm-> gettatol ();
$ Mm-> getpage ();
$ Mm-> num_pl ();
$ Mm-> get_num_pl ();
$ Mm-> showpage ();
$ Mm-> closedatabase ();
$ Mm-> firstpage ();
$ Mm-> backpage ();
$ Mm-> nextpage ();
$ Mm-> footpage ();
?>
This class has been debugged! Very easy to use ~~~~~~~~~~~~~~~~
Class is your knife ~ It's hard to wait until the fight starts !~ Dead ~~
I'm free! Write a functional class ~~~~~~
It should be said that my class ~~ Relatively simple ~~~ Questions you cannot understand ~

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.