Original PHP database operations

Source: Internet
Author: User
Tags php database
Original PHP database operations // This is a tool class to complete database operations;
Class SqlHelper {
Public $ conn;
Public $ dbname = "exam ";
Public $ username = "root ";
Public $ password = "123 ";
Public $ host = "192.168.1.100 ";


// Constructor (connect to the database );
Public function _ construct (){
$ This-> conn = mysql_connect ($ this-> host, $ this-> username, $ this-> password );
If (! $ This-> conn ){
Die ("connection failed". mysql_errno ());
}

// Set the character encoding;
Mysql_set_charset ('utf8 ');
Mysql_select_db ($ this-> dbname, $ this-> conn );
}


// Execute the dql statement
Public function execute_dql ($ SQL ){
$ Res = mysql_query ($ SQL, $ this-> conn) or die (mysql_errno ());
Return $ res;
}

// Execute the dql statement, but an array is returned
Public function execute_dql2 ($ SQL ){
$ Arr = array ();
$ Res = mysql_query ($ SQL, $ this-> conn) or die (mysql_errno ());
$ I = 0;
// Give $ res to $ arr
While ($ row = mysql_fetch_array ($ res )){
$ Arr [$ I] = $ row;
$ I ++;
}
// Close it immediately;
Mysql_free_result ($ res );
Return $ arr;
}


// Execute the dml statement
Public function execute_dml ($ SQL ){
$ B = mysql_query ($ SQL, $ this-> conn) or die (mysql_errno ());
If (! $ B ){
Return 0;
} Else {
If (mysql_affected_rows ($ this-> conn)> 0 ){
Return 1; // indicates that the execution is successful only.
} Else {
Return 2; // indicates that no row is affected;
}
}
}


// Encapsulation
// Queries with paging considerations
// $ SQL = "select * from where table name limit 0, 6 ";
// $ SQL = "select count (*) from table name ";
Public function execute_dql_fenye ($ sql1, $ sql2, $ fenyePage ){
// Obtain the paging data
$ Res = mysql_query ($ sql1, $ this-> conn) or die (mysql_errno ());
// $ Res => array ();
// Transfers data to an array
$ Arr = array ();
While ($ row = mysql_fetch_assoc ($ res )){
$ Arr [] = $ row;
}
// Release resources
Mysql_free_result ($ res );
// Assign an array to $ fenyePage
$ FenyePage-> res_array = $ arr;

$ Res2 = mysql_query ($ sql2, $ this-> conn) or die (mysql_errno ());
If ($ row = mysql_fetch_row ($ res2 )){
$ FenyePage-> pageCount = ceil ($ row [0]/$ fenyePage-> pageSize );
$ FenyePage-> rowCount = $ row [0];
}
Mysql_free_result ($ res2 );

// Encapsulate the navigation information in $ fenyePage;

// Display the previous and next pages
$ Navigate = "";
If ($ fenyePage-> pageNow> 1 ){
$ PrePage = $ fenyePage-> pageNow-1;
$ Navigate = "gotoUrl }? PageNow = $ prePage '> Previous Page ";
}
If ($ fenyePage-> pageNow <$ fenyePage-> pageCount ){
$ NextPage = $ fenyePage-> pageNow + 1;
$ Navigate. = "gotoUrl }? PageNow = $ nextPage '> next page ";
}

// Display the home page and last page
$ Navigate. = "gotoUrl }? PageNow = 1'> homepage ";
$ Navigate. = "gotoUrl }? PageNow = $ fenyePage-> pageCount '> last page ";

// Display the current page and total number of pages
$ Navigate. = "current page {$ fenyePage-> pageNow}/total {$ fenyePage-> pageCount} page ";



// How many pages are displayed below
$ Page_whole = 2;
$ Start = floor ($ fenyePage-> pageNow-1)/$ page_whole) * $ page_whole + 1;
$ Index = $ start;
$ Min = floor ($ fenyePage-> pageCount-1)/$ page_whole) * $ page_whole;
// Flip the entire page forward;
// If the current pageNow is within 10, no page flip occurs;
If ($ fenyePage-> pageNow> $ page_whole ){
$ Navigate. = "gotoUrl }? PageNow = ". ($ start-1)." '> <";
}

// How to define start (1 to 10) (floor (pageNow-1)/10) * 10 + 1; (11 to 20) (floor (pageNow-1/10) * 10 + 1
If ($ fenyePage-> pageNow <= $ min ){
For ($ start; $ start <$ index + $ page_whole; $ start ++ ){
$ Navigate. = "gotoUrl }? PageNow = $ start'> [$ start] ";
}
} Else {
For ($ start; $ start <= $ fenyePage-> pageCount; $ start ++ ){
$ Navigate. = "gotoUrl }? PageNow = $ start'> [$ start] ";
}
}

// Page flip
// Flip the page to display the condition;
//
// $ Min = floor ($ pageCount-1/$ page_whole)
//
// If ($ pageNow <= $ min * $ pagewhole)
//
If ($ fenyePage-> pageNow <= $ min ){
$ Navigate. = "gotoUrl }? PageNow = $ start '>>> ";
}


$ FenyePage-> navigate = $ navigate;
}

// Close the connection
Public function close_connect (){
If (! Empty ($ this-> conn )){
Mysql_close ($ this-> conn );
}
}
}
?>

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.