Mysqli data connection class

Source: Internet
Author: User
Tags mysql tutorial

Mysql tutorial I data connection class
Class Mydb extends mysqli
{
Protected $ _ databasehost = 'localhost ';
Protected $ _ databaseuser = 'root ';
Protected $ _ databasepass = '20140901 ';
Protected $ _ databasename = 'test ';
Protected $ _ databaseport = '20140901 ';
Protected $ _ fetch_method = MYSQLI_ASSOC;
Protected $ _ databasechar = 'utf8 ';

/**
* Enter description here...
*
* @ Param string $ host
* @ Param string $ user
* @ Param string $ pass
* @ Param string $ name
* @ Param int $ port
* @ Param string $ char
*/
Function _ construct ($ host = '', $ user ='', $ pass = '', $ name ='', $ port = '', $ char = '')
{
$ This-> _ databasehost = empty ($ host )? $ This-> _ databasehost: $ host;
$ This-> _ databaseuser = empty ($ user )? $ This-> _ databaseuser: $ user;
$ This-> _ databasepass = empty ($ pass )? $ This-> _ databasepass: $ pass;
$ This-> _ databasename = empty ($ name )? $ This-> _ databasename: $ name;
$ This-> _ databaseport = empty ($ port )? $ This-> _ databaseport: $ port;
$ This-> _ databasechar = empty ($ char )? $ This-> _ databasechar: $ char;
Try {
Parent ::__ construct ($ this-> _ databasehost, $ this-> _ databaseuser,
$ This-> _ databasepass, $ this-> _ databasename,
$ This-> _ databaseport );
If (mysqli_connect_errno ()){
Die ("server connection failed ");
}
If (! Parent: set_charset ($ this-> _ databasechar )){
Die ("the database tutorial cannot use UTF-8 encoding ");
}
} Catch (Exception $ e ){
Die ($ e-> getMessage ());
}
}

Function selectDb ($ name)
{
$ This-> _ databasename = empty ($ name )? $ This-> _ databasename: $ name;
Return $ this-> select_db ($ this-> _ databasename );
}

/**
* Enter description here...
*
*/
Function _ desstruct ()
{
$ This-> close ();
}

/**
* Set the return value type
*
* @ Param int $ type
*/
Function setFetchMethod ($ type = 2)
{
Switch ($ type)
{
Case '1 ':
$ This-> _ fetch_method = MYSQLI_NUM;
Break;
Case '2 ':
$ This-> _ fetch_method = MYSQLI_ASSOC;
Break;
Case '3 ':
$ This-> _ fetch_method = MYSQLI_BOTH;
Break;
Default:
$ This-> _ fetch_method = MYSQLI_ASSOC;
Break;
}
}

/**
* Escape before insertion
*
* @ Param string | array $ str
* @ Return string
*/
Function quoteInto ($ str)
{
If (is_array ($ str )){
Foreach ($ str as $ key => $ val ){
$ Str [$ key] = $ this-> real_escape_string ($ val );
}
} Else {
$ Str = $ this-> real_escape_string ($ str );
}
Return $ str;
}

/**
* Insert
*
* @ Param string $ table
* @ Param array $ fields
* @ Return int
* @ Deprecated the key name is the inserted field value. The key value is the inserted value.
*/
Function insert ($ table, $ fields)
{
If (empty ($ table) {return false ;}
If (empty ($ fields) {return false ;}
$ InsertFields = @ implode (',', @ array_keys ($ fields ));
$ ArrayValues = @ array_values ($ fields );
$ Result = $ this-> quoteInto ($ arrayValues );
$ InsertValues = "'". @ implode ("', '", $ result )."'";
$ SQL = "INSERT INTO". $ table. "(". $ insertFields .")
VALUES (". $ insertValues .")";
$ This-> query ($ SQL );
Return $ this-> insert_id;
}
 

/**
* Update
*
* @ Param string $ table
* @ Param array $ fields
* @ Param string $ where
* @ Return null
* @ Deprecated the key name is the inserted field value. The key value is the inserted value.
*/
Function update ($ table, $ fields, $ where)
{
If (empty ($ table) {return false ;}
If (empty ($ fields) {return false ;}
If (empty ($ where) {return false ;}
Foreach ($ fields as $ key => $ v)
{
$ Condition [] = "{$ key} = '". $ this-> quoteInto ($ v )."'";
}
$ SQL = "UPDATE". $ table ."
SET ". implode (',', $ condition)." WHERE ". $ where;
$ This-> query ($ SQL );
}

/**
* Get a piece of data
*
* @ Param string $ SQL
* @ Return array one-dimensional array
*/
Function fetchRow ($ SQL)
{
$ Result = $ this-> query ($ SQL );
$ Row = $ result-> fetch_array ($ this-> _ fetch_method );
Return $ row;
}

/**
* Get all data
*
* @ Param string $ SQL
* @ Return array two-dimensional array
*/
Function fetchAll ($ SQL)
{
$ Result = $ this-> query ($ SQL );
While ($ row = $ result-> fetch_array ($ this-> _ fetch_method ))
{
$ Data [] = $ row;
}
Return $ data;
}

Function fetchFree ()
{
Return $ this-> free_result ();
}

/**
* Enter description here...
*
* @ Param string $ type
* @ Return unknown
*/
Function getVersion ($ type = 'server ')
{
Switch ($ type ){
Case 'server ':
$ Result = $ this-> server_version;
Break;
Case 'client ':
$ Result = $ this-> client_version;
Break;
Default:
$ Result = $ this-> server_version;
Break;
}
Return $ result;
}

/**
* Enter description here...
*
* @ Param string $ type
* @ Return unknown
*/
Function getInfo ($ type = 'server ')
{
Switch ($ type ){
Case 'server ':
$ Result = $ this-> server_info;
Break;
Case 'client ':
$ Result = $ this-> client_info;
Break;
Default:
$ Result = $ this-> server_info;
Break;
}
Return $ result;
}
}

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.