For mysql database operations, [php] & lt ;? Php/*** db. class. phpcreatedatabseobject *** @ authorDick417@668x.net * @ copyrighthttp: // blog.csdn.net/haibrother?#/classdick_db=pu
Mysql database operations
[Php]
/**
* Db. class. php create databse object
*
* @ Author Dick 417@668x.net
* @ Copyright http://blog.csdn.net/haibrother
*
**/
Class Dick_Db {
Public $ db_host = ''; // host address
Public $ db_username = ''; // database account
Public $ db_password = ''; // database password
Public $ db_name = ''; // database name
Public $ link = ''; // database connection object
Public $ debug = 1; // whether to enable debug. debug is enabled by default.
Public $ pconnect = 0; // whether to enable persistent connections. by default, persistent connections are disabled.
Public $ log_file = 'Log/'; // log file directory
/**
* Initialization information
* @ Param object
**/
Public function _ construct ($ config = ''){
If (! Is_object ($ config )){
$ This-> halt ('database config is not wrong, please check it! ');
}
If (! Empty ($ config )){
$ This-> db_host = $ config-> host;
$ This-> db_username = $ config-> username;
$ This-> db_password = $ config-> password;
$ This-> db_name = $ config-> dbname;
}
$ This-> connect ();
}
/**
* Get link
**/
Public function connect (){
$ Connect = $ this-> pconnect === 1? 'MySQL _ pconnect ': 'MySQL _ connect ';
If (! $ This-> link = @ $ connect ($ this-> db_host, $ this-> db_username, $ this-> db_password )){
$ This-> halt ('database cant not connect! ');
}
Mysql_set_charset ('utf8', $ this-> link );
Mysql_select_db ($ this-> db_name, $ this-> link );
}
/**
* Query
* @ Param string $ SQL
* Return boolean
**/
Public function query ($ SQL ){
If (! $ Query = mysql_query ($ SQL, $ this-> link )){
$ Message = date ('Y-m-d H: I: s'). '. $ SQL;
$ This-> write_log ($ message );
$ This-> halt ('SQL error, please check it! ', $ SQL );
}
Return $ query;
}
/**
*
* @ Param string $ SQL
* @ Param string $ type
* Mysql_fetch_assoc mysql_fetch_array mysql_fetch_row mysql_affected_rows
* @ Return array
*/
Public function fetch ($ SQL, $ type = 'assoc '){
$ Fetch_type = 'MySQL _ fetch _ '. $ type;
$ Query = $ this-> query ($ SQL );
$ Result = $ fetch_type ($ query );
$ This-> free_result ($ query );
$ This-> close ();
Return $ result;
}
/**
* @ Param string $ SQL
* @ Return array
**/
Public function dickFetch ($ SQL, $ type = 'assoc '){
$ Fetch_type = 'MySQL _ fetch _ '. $ type;
$ Query = $ this-> query ($ SQL );
$ Rows = array ();
While ($ row = $ fetch_type ($ query )){
$ Rows [] = $ row;
}
$ This-> free_result ($ query );
$ This-> close ();
Return $ rows;
}
/**
* Get the last insert ID
* @ Param string $ SQL
**/
Public function insert_id (){
Return mysql_insert_id ($ this-> link );
}
/**
* Release Database object resources
**/
Public function free_result ($ query ){
Return mysql_free_result ($ query );
}
/**
* Close database connection
**/
Public function close (){
If ($ this-> pconnect === 0) return mysql_close ($ this-> link );
}
/**
* For integer security processing, only an integer greater than or equal to 0 is returned.
* @ Param int
* @ Return int
**/
Public function numeric (& $ variint ){
If (! Isset ($ variint ))
Return 0;
If (! Is_numeric ($ variint ))
Return 0;
// Starts with 0
$ Str_len = strlen ($ variint );
For ($ I = 0; $ I <$ str_len; $ I ++ ){
If ($ variint [$ I]! = '0 ')
Break;
}
If ($ I> 0 & $ variint> 0 ){
$ Variint = substr ($ variint, $ I, $ str_len );
$ Str_len = strlen ($ variint );
}
// Digital security processing
If ($ str_len> 0 ){
If (! Preg_match ("/^ [0-9] + $/", $ variint )){
Return 0;
} Else {
$ Variint = substr ($ variint, 0, 10 );
// Compatible with MYSQL's INT unsigned maximum value of 4294967295
$ Variint = ($ variint> 4294967295 )? 4294967295: $ variint;
Return $ variint;
}
} Else {
Return 0;
}
}
/**
* Mysql error is returned.
**/
Public function error (){
Return ($ this-> link )? Mysql_error ($ this-> link): mysql_error ());
}
/**
* Mysql errno is returned.
**/
Public function errno (){
Return intval ($ this-> link )? Mysql_errno ($ this-> link): mysql_errno ());
}
/**
* Write SQL error logs
* @ Param string
* @ Param string type
**/
Public function write_log ($ message = '', $ type = 'Date '){
If (empty ($ message) return false;
If (! In_array ($ type, array ('date', 'month') return false;
If (! Is_dir ($ this-> log_file )){
Mkdir ($ this-> log_file );
}
Switch ($ type ){
Case 'month ':
$ File = $ this-> log_file.date ('Y-M'). '. log ';
Break;
Default:
$ File = $ this-> log_file.date ('Y-m-D'). '. log ';
Break;
}
If (! File_exists ($ file )){
File_put_contents ($ file ,'');
}
If (! Is_readable ($ file )){
$ This-> halt ($ file. '-the system has no read permission! ');
}
If (! Is_writable ($ file )){
$ This-> halt ($ file. '-the system has no write permission! ');
}
$ Contents = file_get_contents ($ file );
$ Add_message = '-Error:'. $ this-> error (). '-Errno:'. $ this-> errno ();
File_put_contents ($ file, $ contents. $ message. $ add_message. "\ n ");
}
/**
* Terminate the program
* @ Param str $ message
* @ Return print
**/
Public function halt ($ message = '', $ SQL = ''){
If ($ this-> debug = 1 ){
$ Error_get_last = error_get_last ();
If ($ message ){
$ Errmsg ="System info: $ Message \ n ";
}
$ Errmsg. ="Time: ". Date ('Y-m-d H: I: s')." \ n ";
$ Errmsg. ="Script: ". $ Error_get_last ['file']." \ n ";
If ($ SQL ){
$ Errmsg. ="SQL: ". Htmlspecialchars ($ SQL)." \ n ";
}
$ Errmsg. ="Error: ". $ This-> error ()." \ n ";
$ Errmsg. ="Errno.: ". $ This-> errno ();
Echo"\ N ";
Echo"
";
Echo nl2br ($ errmsg );
Exit ();
}
}
/**
* Program test printing
* @ Param string
* @ Return print
**/
Public function prf ($ param ){
Echo'
';
print_r($param);
echo '
';
Exit;
}
}
######################################## #########
#### Test program
####
######################################## ##########
Error_reporting (E_ALL );
Header ('content-type: text/html; charset = utf-8 ');
Date_default_timezone_set ('Asia/Shanghai ');
$ Config = array (
'Host' => '2017. 168.2.1 ',
'Username' => 'root ',
'Password' => '',
'Dbname' => 'test ',
);
$ Db = new Dick_Db (object) $ config );
$ Db-> pconnect = 1;
$ SQL = 'select * FROM T1 ';
$ Db-> prf ($ db-> dickFetch ($ SQL ));
?>