Database encapsulation and reference 1. database encapsulation and reference
The previous code was written through mysql to get the result. the disadvantage of this writing is that SQL parameters need to be written every time data is used, and many copies need to be written. when the database is changed, or if the password is changed, you need to modify mysql-related parameters in each file. The results will be very complex. The following encapsulates database operations into a class, and calls the encapsulated class during each operation.
In the future, write data using encapsulation classes to prevent changing database names, user names, and passwords from the encapsulated classes after changing the database, changing the computer, or putting the data on the server. You do not need to modify the content on the page.
1. query statement
Step 2: Create an encapsulation class file DBDA. class. php
Host "," $ this-> uid "," $ this-> pwd "," $ this-> dbname "); // execute the SQL statement $ result = $ db-> query ("$ SQL"); // obtain data from the result set object. Query is used as a separate method, while others are used as another method. If ($ type = 1) // if the query is {return $ result-> fetch_all (); // returns the queried two-dimensional array} else // if it is an added or modified {return $ result; // returns $ result }}}
Step 2: Reference the encapsulated class to the page
Untitled Document
Query ($ SQL); // if no 2nd parameters are specified, Query is performed because the default value is 1.?>
If it is added, deleted, and modified, the return value is boolean flase.
Untitled Document
Query ($ SQL); // if no 2nd parameters are specified, Query is performed because the default value is 1. */$ SQL = "update info set name = 'Yang Ningbo 'where code = 'p001'"; var_dump ($ db-> Query ($ SQL, 0);?>
Associate the output result with an array.
Step 2: encapsulation class
Host, $ this-> uid, $ this-> pdw, $ this-> dbname); // execute the SQL statement $ result = $ db-> query ($ SQL ); // retrieve data from the result set object. Query is used as a separate method, while others are used as another method. If ($ type = 1) // if the query is {return $ result-> fetch_all (); // return the query's two-dimensional array} else // if it is added or modified {return $ result; // return $ result} // 2. returns the join array method/*** to an SQL statement, returns the associated two-dimensional array * @ param string $ the SQL statement specified by the SQL user * @ param int $ the type of statement given by the SQL user. 0 indicates addition, deletion, modification, and 1 indicates query. Generally, the query uses a lot, so the default value of $ type is 1. if it is added, deleted, modified, and then changed to the value of $ type. * @ Return array returns the query result. if it is a query, a two-dimensional array is returned. If it is added, deleted, or modified, $ result is returned. */Function GuanQuery ($ SQL, $ type = 1) {// create a connection object $ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pdw, $ this-> dbname); // execute the SQL statement $ result = $ db-> query ($ SQL ); // Obtain data if ($ type = 1) // if $ type = 1 {$ attr = array (); while ($ a = $ result-> fetch_assoc ()) // each time an associated array is returned, it must be assembled into a two-dimensional array. {$ Attr [] = $ a; // put the obtained $ a in the array} return $ attr; // returns a two-dimensional correlated array} else {return $ result; // if $ type is not equal to 1, $ result is returned. }}}
Step 2: reference a file to the page
Untitled Document
GuanQuery ($ SQL); // If 2nd parameters are left blank, query is performed because the default value is 1. // $ SQL = "update info set name = 'Yang Ningbo 'where code = 'p001'"; // var_dump ($ db-> Query ($ SQL, 0);?>
Make the output result a string
Step 2: encapsulation class
Host, $ this-> uid, $ this-> pwd, $ this-> dbname); // execute the SQL statement $ reslut = $ db-> query ($ SQL ); // obtain data from the result set object if ($ type = 1) {return $ reslut-> fetch_all () ;} else {return $ reslut ;}} /*** return an Associated two-dimensional array for an SQL statement * @ param string $ the SQL statement specified by the SQL user * @ param int $ type, 0 indicates addition, deletion, modification, and 1 indicates query * @ return. If query is used, a two-dimensional array is returned. if it is added, deletion, modification, or false */function GuanQuery ($ SQL, $ type = 1) {// create a connection object $ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname); // execute the SQL statement $ reslut = $ db-> query ($ SQL); // obtain the data if ($ type = 1) {$ attr = array (); while ($ a = $ reslut-> fetch_assoc () {$ attr [] = $ a;} return $ attr ;} else {return $ reslut;}/*** to an SQL statement, returns the string * @ param string $ the SQL statement specified by the SQL user * @ param int $ type. The value 0 indicates addition, deletion, and modification, 1 indicates query * @ return: return the query result. if it is a query return string, if it is an addition, deletion, modification, return true or false */function StrQuery ($ SQL, $ type = 1) {// create a connection object $ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname ); // execute the SQL statement $ reslut = $ db-> query ($ SQL); // obtain the data if ($ type = 1) {$ attr = $ reslut-> fetch_all (); $ str = ""; foreach ($ attr as $ v) {$ str. = implode ("^", $ v); $ str. = "|";} return substr ($ str, 0, strlen ($ str)-1);} else {return $ reslut ;}}}
Step 2: Reference class
Untitled Document
StrQuery ($ SQL); // If 2nd parameters are not written, query is performed because the default value is 1. // $ SQL = "update info set name = 'Yang Ningbo 'where code = 'p001'"; // var_dump ($ db-> Query ($ SQL, 0 )); // is a string containing all the data found?>
2. if you need to use more pages, you can try writing it yourself after learning ajax. Now we use the following tool class for paging. writing is complicated, but it is very simple to use. The code is as follows:
"Record", 'prev' => "previous page", 'next' => "next page", 'first' => "homepage ", 'last' => "last page"); // display the content in the page information. you can set private $ listNum = 10 by using the set () method; // The number of records displayed in the default page List/** constructor. you can set the attribute @ param int $ total of the page class to calculate the total number of records @ param int $ listRows (optional, set the number of records to be displayed on each page. the default value is 25 @ param mixed $ query (optional). It is an array used to pass parameters to the target page, it can also be an optional string query format @ param bool $ ord. the default value is true. the page is displayed from the first page, false indicates the last page */public function _ construct ($ total, $ listRows = 25, $ query = "", $ Ord = true) {$ this-> total = $ total; $ this-> listRows = $ listRows; $ this-> uri = $ this-> getUri ($ query); $ this-> pageNum = ceil ($ this-> total/$ this-> listRows ); /* the following judgment is used to set the current surface */if (! Empty ($ _ GET ["page"]) {$ page = $ _ GET ["page"];} else {if ($ ord) $ page = 1; else $ page = $ this-> pageNum;} if ($ total> 0) {if (preg_match ('/\ D/', $ page )) {$ this-> page = 1;} else {$ this-> page = $ page ;}} else {$ this-> page = 0 ;} $ this-> limit = "LIMIT ". $ this-> setLimit ();}/** is used to set the display page information, you can perform a coherent operation @ param string $ param is the subscript of the member attribute array config @ param string $ value is used to set the element value corresponding to the config subscript @ return object to return the current object $ this, used for connection operations */function set ($ param, $ value) {if (array_key_exists ($ param, $ this-> config )) {$ this-> config [$ param] = $ value;} return $ this;}/* is not called directly. through this method, you can directly obtain the limit and page values of private member attributes outside the object */function _ get ($ args) {if ($ args = "limit" | $ args = "page") return $ this-> $ args; else return null ;} /** output pagination @ param int 0-7 numbers in the specified format as parameters for customizing the output pagination structure and adjusting the structure order, all structure output by default @ return string paging information content */function fpage () {$ arr = func_get_args (); $ html [0] = "total{$ This-> total}{$ This-> config ["head"]} "; $ html [1] =" this page". $ This-> disnum ().""; $ Html [2] =" this page is from{$ This-> start ()}-{$ this-> end ()}"; $ Html [3] ="{$ This-> page}/{$ this-> pageNum}Page "; $ html [4] = $ this-> firstprev (); $ html [5] = $ this-> pageList (); $ html [6] = $ this-> nextlast (); $ html [7] = $ this-> goPage (); $ fpage =''; If (count ($ arr) <1) $ arr = array (0, 1, 2, 3, 4, 5, 6, 7); for ($ I = 0; $ I <count ($ arr); $ I ++) $ fpage. = $ html [$ arr [$ I]; $ fpage. ='
'; Return $ fpage;}/* private method used inside the object, */private function setLimit () {if ($ this-> page> 0) return ($ this-> page-1) * $ this-> listRows. ", {$ this-> listRows}"; else return 0;}/* Private method used inside the object, used to automatically obtain the current URL */private function getUri ($ query) {$ request_uri = $ _ SERVER ["REQUEST_URI"]; $ url = strstr ($ request_uri ,'? ')? $ Request_uri: $ request_uri .'? '; If (is_array ($ query) $ url. = http_build_query ($ query); else if ($ query! = "") $ Url. = "&". trim ($ query ,"? & "); $ Arr = parse_url ($ url); if (isset ($ arr [" query "]) {parse_str ($ arr [" query "], $ arrs); unset ($ arrs ["page"]); $ url = $ arr ["path"]. '? '. Http_build_query ($ arrs);} if (strstr ($ url ,'? ') {If (substr ($ url,-1 )! = '? ') $ Url = $ url.' & ';} else {$ url = $ url .'? ';} Return $ url;}/* private method used inside the object to obtain the number of records starting from the current page */private function start () {if ($ this-> total = 0) return 0; else return ($ this-> page-1) * $ this-> listRows + 1 ;} /* private method used inside the object to obtain the number of records ending on the current page */private function end () {return min ($ this-> page * $ this-> listRows, $ this-> total);}/* Private method used inside the object, used to obtain operation information of the previous page and homepage */private function firstprev () {if ($ this-> page> 1) {$ str = "uri} page = 1' >{$ this-> config [" first "]}"; $ str. = "uri} page = ". ($ this-> page-1 ). "'>{$ this-> config [" prev "]}"; return $ str ;}}/* Private method used inside the object, used to obtain page list information */private function pageList () {$ linkPage =""; $ Inum = floor ($ this-> listNum/2);/* list before the current page */for ($ I = $ inum; $ I> = 1; $ I --) {$ page = $ this-> page-$ I; if ($ page> = 1) $ linkPage. = "uri} page = {$ page} '> {$ page}";}/* Current page information */if ($ this-> pageNum> 1) $ linkPage. = "{$ this-> page}";/* list after the current page */for ($ I = 1; $ I <= $ inum; $ I ++) {$ page = $ this-> page + $ I; if ($ page <= $ this-> pageNum) $ linkPage. = "uri} page = {$ page} '> {$ page}"; else break;} $ linkPage. =''; Return $ linkPage;}/* private method used inside the object to obtain operation information on the next and last pages */private function nextlast () {if ($ this-> page! = $ This-> pageNum) {$ str = "uri} page = ". ($ this-> page + 1 ). "'>{$ this-> config [" next "]}"; $ str. = "uri} page = ". ($ this-> pageNum ). "'>{$ this-> config [" last "]}"; return $ str ;}}/* Private method used inside the object, used to display and process the form jump page */private function goPage () {if ($ this-> pageNum> 1) {return'';}}/* Private method used inside the object to obtain the number of records displayed on this page */private function disnum () {if ($ this-> total> 0) {return $ this-> end ()-$ this-> start () + 1 ;}else {return 0 ;}}}
The following is a simple example of using the pagination class.
Untitled Document
StrQuery ($ sz); // Create a paging object $ page = new Page ($ zts, 20); // 20 pieces of data per page. $ SQL = "select * from chinastates". $ page-> limit; // query by page. $ page-> limit to view page. class. php.
= ->Query( "
"( "
| {[0]} |
{[1]} |
{[2]} |
" "
" ->fpage(); ?>