This is a Class I wrote by myself. You are welcome to discuss and guide it. Some usage methods: // Declare the database object $ Conn = new Mysql; // Load database parameters $ Conn-> Parameter (database server, database username, database password, database name, database code, database table prefix [can be blank]);
The above code has loaded this class file and initialized some database connection parameters! The following describes several basic methods and functions: 1. $ Conn-> Table (); Select a data table. the parameter is the data table name. 2. $ Conn-> Field (); Names of the selected fields separated by commas (,). if this method is not called, all fields are returned. 3. $ Conn-> Where (); SQL Where substatement, filtering by conditions 4. $ Conn-> Order (); SQL sorting 5. $ Conn-> Page (int ); The parameter is a positive integer. if you call this method, the record is displayed on pages. 6. $ Conn-> Select (Boolean ); Execute the query and return the query result. If yes, it is a two-dimensional array. If no, it returns false. the parameter can be omitted. if omitted, it is true by default. the returned array contains numeric elements. 7. $ Conn-> Del (); Delete record 8. $ Conn-> Edit (array ()); Modify record. the parameter is a one-dimensional array. the array key is the field name, and the array value is the field value. 9. $ Conn-> Into (array ()); Add record. the parameter is a one-dimensional array, the array key is the field name, and the array value is the field value.
The preceding methods can be called consecutively, for example:
$ Rult = $ Conn-> Table ('user')-> Select (); // query returns all records of the user Table. $ Rult = $ Conn-> Table ('user')-> Page (20)-> Select (); // query all records in the user Table and display them on pages, 20 entries per page;
There are a lot of methods to call and learn more!
- /*
- Database operations
- Operation method: Face objects
- Production time:
- Current version: BqhMysql (mysqli) 1.0.1
- Software author: Sad Songs
- Contact QQ: 36975.
- Contact number: 18907975647
- Email: admin@q128.com
- Contact Address: Xiaosong town, Shicheng county, Ganzhou city, Jiangxi province
- Zip code: 342706
- */
- Class Mysql {
- Private $ LocalHost = 'localhost ';
- Private $ LoaclUser = 'root ';
- Private $ LocalPass = '20140901 ';
- Private $ LocalBase = 'jiangxibaiyi ';
- Private $ LocalCode = 'utf8 ';
- Private $ PreFix;
- Private $ Conn;
- Private $ Start = 0;
- Private $ Error = false; // database connection status; false indicates no connection or the connection is abnormal
- Public $ Err = true; // SQL execution result
- Private $ Table;
- Private $ Field = '*';
- Private $ Where = '';
- Private $ Order = '';
- Private $ PageSize = 0; // display by page-> how many entries per page, 0 is not displayed by page
- Private $ PageCount = 1; // display by page-> Total number of items
- Private $ PageNum = 1; // display by page-> Total number of pages
- Private $ PageNo = 1; // display by page-> current page
- Private $ PageKey = 'page'; // PageKey
- Private $ PageStart = 0; // display by page-> The number of returned results starting from
- Private $ Select;
- Private $ Rest;
- Private $ Result = false; // Result set
- Public $ FormArray = array ();
- Public $ region _id = 0;
- Private $ j = 0;
- Public function Parameter ($ Loca, $ Root, $ Pass, $ Base, $ Code, $ PreFix = ''){
- $ This-> LoaclUser = $ Root;
- $ This-> LocalBase = $ Base;
- $ This-> LocalCode = $ Code;
- $ This-> LocalHost = $ Loca;
- $ This-> LocalPass = $ Pass;
- $ This-> PreFix = $ PreFix;
- Return $ this;
- }
- Private function Connection ($ SQL ){
- ! Function_exists (mysqli_connect )? Die ('query failed, unable to load mysqli extension'): null;
- $ This-> Conn = @ new mysqli ($ this-> LocalHost, $ this-> LoaclUser, $ this-> LocalPass, $ this-> LocalBase );
- $ This-> Error = mysqli_connect_errno () = 0? True: false;
- ! $ This-> Error? Die ('database connection error, check database connection parameters'): null;
- $ This-> Conn-> query ('set names'. $ this-> LocalCode );
- $ This-> Rest = $ this-> Conn-> query ($ SQL );
- $ This-> Err = mysqli_error ($ this-> Conn );
- $ This-> consumer _id = mysqli_insert_id ($ this-> Conn );
- $ This-> Rest-> free_result;
- $ This-> Conn-> close;
- $ This-> FormArray = '';
- Return $ this;
- }
- Public function null (){
- $ This-> PageSize = 0;
- // $ This-> PageCount = 1;
- $ This-> PageStart = 1;
- $ This-> Field = '*';
- $ This-> Select = '';
- Unset ($ this-> Table, $ this-> Where, $ this-> Order, $ this-> Result );
- }
- Public function Table ($ TableName) {// data Table
- $ This-> null ();
- $ This-> Table = '''. $ this-> PreFix. $ TableName .''';
- Return $ this;
- }
- Public function Field ($ Array = '*') {// data Field
- ! Empty ($ this-> Field )? $ This-> Field = '': null;
- $ Array = explode (',', $ Array );
- Foreach ($ Array as $ field ){
- $ This-> Field. =! $ This-> Start? '''. $ Field. ''': ', ''. $ field .''';
- $ This-> Start ++;
- }
- $ This-> Start = 0;
- Return $ this;
- }
- Public function Where ($ Where) {// condition
- $ This-> Where = 'where'. $ where;
- Return $ this;
- }
- Public function Order ($ Order) {// sort
- $ This-> Order = 'Order by'. $ order;
- Return $ this;
- }
- Public function pk ($ key) {// pagination url parameter key
- $ This-> PageKey = $ key;
- Return $ this;
- }
- Public function Page ($ PageSize) {// Page
- $ This-> PageSize = $ PageSize;
- $ This-> PageNo = $ this-> get ($ this-> PageKey );
- $ This-> PageNo = empty ($ this-> PageNo) |! Isset ($ this-> PageNo) |! Is_numeric ($ this-> PageNo) | $ this-> PageNo <1? 1: $ this-> PageNo;
- Return $ this;
- }
- Public function post ($ Key, $ Filter = true ){
- Return $ Filter? Strip_tags ($ _ POST [$ Key]): $ _ POST [$ Key];
- }
- Public function get ($ Key, $ Filter = true ){
- Return $ Filter? Strip_tags ($ _ GET [$ Key]): $ _ GET [$ Key];
- }
- Public function Sel (){
- $ This-> Select = 'select'. $ this-> Field. 'from'. $ this-> Table. $ this-> Where. $ this-> Order;
- $ This-> Connection ($ this-> Select );
- If ($ this-> Rest-> num_rows ){
- While ($ Rs = $ this-> Rest-> fetch_assoc ()){
- $ This-> Result [] = $ Rs;
- }
- }
- $ DataBase = $ this-> Result;
- Return empty ($ DataBase )? False: $ DataBase;
- }
- Public function querys ($ SQL = '', $ Type = 'not', $ biao = false ){
- $ This-> Select = $ SQL;
- $ This-> Connection ($ this-> Select );
- If ($ this-> Rest-> num_rows ){
- If (! $ Biao ){
- While ($ Rs = $ this-> Rest-> fetch_array ()){
- $ This-> Result [] =! Preg_match ('/^ \ d + $/I', $ Type )? $ Rs: $ Rs [$ Type];
- }
- } Else {
- While ($ Rs = $ this-> Rest-> fetch_assoc ()){
- $ This-> Result [] = $ Rs;
- }
- }
- }
- $ DataBase = $ this-> Result;
- Return empty ($ DataBase )? False: $ DataBase;
- }
- Public function executes ($ SQL = ''){
- $ This-> Connection ($ SQL );
- Return $ this-> Rest;
- }
- Public function exists ($ T = '', $ F ='', $ W = ''){
- If (empty ($ F) {return 0 ;}
- $ Cmd = empty ($ W )? 'Select sum ('. $ F. ') as 'policyinum' from ''. $ this-> PreFix. $ T. ''': 'Select sum ('. $ F. ') as 'policyinum' from ''. $ this-> PreFix. $ T. ''where '. $ W;
- $ This-> Connection ($ cmd );
- Unset ($ T, $ F, $ W, $ cmd );
- $ Rel = $ this-> Rest-> fetch_array ();
- Return round ($ Rel ['categoryinum'], 2 );
- }
- Public function ExistsTo ($ Bili = 10000, $ T = '', $ F ='', $ W = ''){
- If (empty ($ F) {return 0 ;}
- $ Cmd = empty ($ W )? 'Select sum ('. $ F. ') as 'policyinum' from ''. $ this-> PreFix. $ T. ''': 'Select sum ('. $ F. ') as 'policyinum' from ''. $ this-> PreFix. $ T. ''where '. $ W;
- $ This-> Connection ($ cmd );
- Unset ($ T, $ F, $ W, $ cmd );
- $ Rel = $ this-> Rest-> fetch_array ();
- Return round ($ Rel ['policyinum'] * $ Bili );
- }
- Public function Select ($ Type = true, $ ListNum = 1) {// return record (array format, number of returned Records)
- $ This-> Select = 'select'. $ this-> Field. 'from'. $ this-> Table. $ this-> Where. $ this-> Order;
- If (is_numeric ($ ListNum )){
- If ($ this-> PageSize> 0 ){
- $ This-> Connection ($ this-> Select); // execute the query
- $ This-> PageCount = $ this-> Rest-> num_rows; // gets the total number of records.
- $ This-> PageNum = ceil ($ this-> PageCount/$ this-> PageSize); // total number of pages
- $ This-> PageNo = $ this-> PageNo> $ this-> PageNum? $ This-> PageNum: $ this-> PageNo;
- $ This-> PageStart = ($ this-> PageNo-1) * $ this-> PageSize; // The number of items returned from the current start
- $ This-> Select. = 'limit'. $ this-> PageStart. ','. $ this-> PageSize; // re-construct an SQL statement
- } Else {
- $ This-> Select. = 'limit '. $ ListNum; // reconstructs an SQL statement.
- }
- } Else {
- $ This-> Select. = 'limit 1'; // reconstructs an SQL statement.
- }
- // Echo $ this-> Select;
- $ This-> Connection ($ this-> Select); // run the query again
- If ($ this-> Rest-> num_rows) {// if the record exists
- If ($ Type ){
- While ($ Rs = $ this-> Rest-> fetch_array ()){
- $ This-> Result [] = $ Rs;
- }
- } Else {
- While ($ Rs = $ this-> Rest-> fetch_assoc ()){
- $ This-> Result [] = $ Rs;
- }
- }
- }
- If ($ ListNum = 1 or! Is_numeric ($ ListNum ))&&! $ This-> PageSize) {$ this-> Result = $ this-> Result [0];}
- $ DataBase = $ this-> Result;
- Return empty ($ DataBase )? False: $ DataBase;
- }
- Public function Num () {// total number of returned Records
- $ This-> Select = 'select'. $ this-> Field. 'from'. $ this-> Table. $ this-> Where. $ this-> Order;
- $ This-> Connection ($ this-> Select); // execute the query
- Return $ this-> Rest-> num_rows; // Retrieve the total number of records
- }
-
- Public function PageNav ($ NumNav = false) {// pagination
- $ Action = $ this-> get ('action ');
- ! Empty ($ Action) or $ Action = 'index ';
- $ Module = $ this-> get ('module ');
- ! Empty ($ Module) or $ Module = 'index ';
- $ NavUrl = '/'. $ Module. '/'. $ Action. '/'. $ this-> PageKey .'/';
- $ NaIndex = '/'. $ Module. '/'. $ Action;
- $ PageHtml = "\ n
";
- $ PageHtml. = ''. $ this-> PageCount. 'record '. $ this-> PageNo.'/'. $ this-> PageNum. 'page ';
- $ This-> PageNo <= 1 or $ PageHtml. = "homepage \ nPageNo-1)." \ "> Previous Page \ n ";
- If ($ NumNav) {$ PageHtml. = $ this-> NumPage ($ NavUrl );}
- $ This-> PageNo >=$ this-> PageNum or $ PageHtml. = "PageNo + 1 ). "\"> Next Page \ nPageNum. "\"> Last Page \ n ";
- $ PageHtml. ="\ N ";
- Return $ PageHtml;
- }
-
- Private function NumPage ($ Can = '') {// numeric page
- $ NumHtml = '';
- $ First = 1;
- $ Last = $ this-> PageNum;
- If ($ this-> PageNum> 5 ){
- If ($ this-> PageNo <$ this-> PageNum ){
- $ First = $ this-> PageNo-2;
- $ Last = $ this-> PageNo + 2;
- } Else {
- $ First = $ this-> PageNo-4;
- $ Last = $ this-> PageNum;
- }
- }
- If ($ First <1) {$ First = 1; $ Last = $ First + 4 ;}
- If ($ Last> $ this-> PageNum) {$ First = $ this-> PageNum-4; $ Last = $ this-> PageNum ;}
- For ($ I = $ First; $ I <= $ Last; $ I ++ ){
- $ NumHtml. = $ this-> PageNo! = $ I? "\ N \ t ". ''. $ I. ''. "\ n \ t": "\ n \ t ". ''. $ I. ''. "\ n \ t ";
- }
- Unset ($ Can, $ First, $ I, $ Last );
- Return $ NumHtml;
- }
-
- Public function UserPage ($ NumNav = false, $ PageName = 'index', $ Mulu = 'user') {// Member Center page
- $ NavUrl = '/'. $ Mulu. '/'. $ PageName. '/'. $ this-> PageKey .'/';
- $ PageHtml = "\ n
";
- $ PageHtml. = ''. $ this-> PageCount. 'record '. $ this-> PageNo.'/'. $ this-> PageNum. 'page ';
- $ This-> PageNo <= 1 or $ PageHtml. = "homepage \ nPageNo-1)." \ "> Previous Page \ n ";
- If ($ NumNav) {$ PageHtml. = $ this-> NumPage ($ NavUrl );}
- $ This-> PageNo >=$ this-> PageNum or $ PageHtml. = "PageNo + 1 ). "\"> Next Page \ nPageNum. "\"> Last Page \ n ";
- $ PageHtml. ="\ N ";
- Return $ PageHtml;
- }
-
-
- // Form processing starts
-
- // Submit when judging the form
- Public function FormIs ($ Keys = 'mm '){
- Return $ _ POST [$ Keys] = 1? True: false;
- }
- // Obtain data in post mode
- Public function _ post ($ Keys = '', $ TiHuan = ''){
- $ Values = strip_tags ($ _ POST [$ Keys]);
- $ This-> FormArray [$ Keys] = empty ($ Values )? $ TiHuan: $ Values;
- Return empty ($ Values )? $ TiHuan: $ Values;
- }
- // Get the data
- Public function _ get ($ Keys = '', $ TiHuan = ''){
- $ Values = strip_tags ($ _ GET [$ Keys]);
- Return empty ($ Values )? $ TiHuan: $ Values;
- }
- // Determine whether the value is a number and not less than 0
- Public function IsNum ($ Num = 0, $ Mesg = 'parameter must be a number '){
- If (is_numeric ($ Num )&&! Empty ($ Num) & $ Num> = 0 ){
- Return $ Num;
- } Else {
- Die ($ Mesg );
- }
- }
- // Returns True/False if it is a number and is not less than 0.
- Public function NumBer ($ Num = 0 ){
- Return is_numeric ($ Num )&&! Empty ($ Num) & $ Num> = 0? True: false;
- }
- // Detection data seems to exist
- Public function IsData ($ Types = true, $ memg = 'data already exists '){
- $ This-> Connection ('select'. $ this-> Field. 'from'. $ this-> Table. $ this-> Where );
- If ($ Types ){
- $ This-> Rest-> num_rows> 0? Die ($ memg): null;
- } Else {
- Return $ this-> Rest-> num_rows;
- }
- }
- // Write database records
- Public function into ($ Mesg = ''){
- ! Is_array ($ this-> FormArray )? Die ($ Mesg): null;
- $ SQL = 'insert'. $ this-> Table .'('';
- $ I = 0;
- Foreach ($ this-> FormArray as $ Key => $ Val ){
- $ Duan. =! $ I? $ Key. ''': ', ''. $ Key .''';
- If (is_numeric ($ Val )){
- $ Vals. =! $ I? $ Val: ','. $ Val;
- } Else {
- $ Vals. =! $ I? '\ ''. $ Val.' \'': ', \ ''. $ Val .'\'';
- }
- $ I ++;
- }
- $ SQL. = $ Duan. ') values ('. $ Vals .')';
- // @ File_put_contents ('1. SQL ', $ SQL, FILE_APPEND );
- $ This-> Connection ($ SQL );
- Return! Empty ($ this-> Err )? False: true;
- }
- // Write data in array form
- Public function MsgBox ($ Table = '', $ Filed = array ()){
- $ This-> Table ($ Table );
- Foreach ($ Filed as $ Key => $ Val ){
- $ This-> FormArray [$ Key] = $ Val;
- }
- Return $ this-> Into ('data not obtained ');
- }
-
- // Modify database records
- Public function Edit ($ Array = array ()){
- If (empty ($ Array) {$ Array = $ this-> FormArray ;}
- If (! Is_array ($ Array) | empty ($ Array )){
- Return false;
- } Else {
- $ SQL = 'update'. $ this-> Table. 'set ';
- $ I = 0;
- $ Sub = '';
- $ Huan = array ('-' => '[jian]', '+' => '[jia]', '*' => '[cheng]', '/' => '[chu]');
- $ Zhan = array ('[jian]' => '-', '[jia]' => '+', '[cheng]' => '*', '[chu]' => '/');
- Foreach ($ Array as $ Files => $ Val ){
- $ Val =! Is_numeric ($ Val )&&! Preg_match ('/\' \ w + \ '\ s * (\ + | \-| \ * | \/)/I', $ Val )? '\ ''. $ Val.' \'': $ Val;
- Foreach ($ Huan as $ key => $ val ){
- $ Val = str_replace ($ key, $ val, $ Val );
- }
- $ Duan =! $ I? '''. $ Files. ''= ':',''. $ Files. ''= ';
- $ Sub. = $ duan. $ Val;
- $ I ++;
- }
- $ SQL. = $ Sub. $ this-> Where;
- Foreach ($ Zhan as $ Fan =>$ Hui ){
- $ SQL = str_replace ($ Fan, $ Hui, $ SQL );
- }
-
- // Echo $ SQL; die;
-
- $ This-> Connection ($ SQL );
- Unset ($ Array, $ duan, $ Fan, $ Files, $ Huan, $ Hui, $ I, $ key, $ SQL, $ Sub, $ Val, $ Zhan, $ val );
- Return! Empty ($ this-> Err )? False: true;
- }
- }
- // Delete database records
- Public function del (){
- $ SQL = 'delete from'. $ this-> Table. $ this-> Where;
- $ This-> Connection ($ SQL );
- Unset ($ SQL );
- Return! Empty ($ this-> Err )? False: true;
- }
-
- // Form processing ends
-
- // Page jump
- Public function Msg ($ Text = 'Operation succeeded '){
- Echo' ';
- Echo'
|