'); $this->pconnect= $pconnect; $this->dblink= $pconnect? mysql_pconnect ($hostname, $username, $userpwd): mysql_connect ($hostname, $username, $ USERPWD); (! $this->dblink| |! Is_resource ($this->dblink)) && fatal_error ("Connect to the Database unsuccessfully!"); @mysql_unbuffered_query ("Set names {$charset}"); if ($this->version () > ' 5.0.1 ') { @mysql_unbuffered_query ("Set sql_mode = '"); } @mysql_select_db ($database) or fatal_error ("Can not select table!"); return $this->dblink; } function query ($sql, $unbuffered =false) { echo $sql. ' '; $this->rs= $unbuffered mysql_unbuffered_query ($sql, $this->dblink): mysql_query ($sql, $this->dblink); (! $this->rs| |! Is_resource ($this->rs)) && fatal_error ("Execute the query unsuccessfully! Error: ". Mysql_error ()); if (! $this->rs) fatal_error (' Execute SQL statement '. $sql. ' When the following error occurred: '. mysql_error ()); return $this->rs; } function Fetch_one ($sql) { $this->rs= $this->query ($sql); return dircms_strips Tutorial Lashes ($this->filter_pass (mysql_fetch_array ($this->rs,mysql_assoc)); } function Get_maxfield ($filed = ' id ', $table)//Get the maximum value of the $filed field in the $table table { $r = $this->fetch_one ("Select {$table}.{ $filed} from ' {$table} ' order by ' {$table} '. ' {$filed} ' desc limit 0,1 '); return $r [$filed]; } function Fetch_all ($sql) { $this->rs= $this->query ($sql); $result =array (); while ($rows =mysql_fetch_array ($this->rs,mysql_assoc)) { $result []= $rows; } Mysql_free_result ($this->rs); Return Dircms_stripslashes ($this->filter_pass ($result)); } function Fetch_all_withkey ($sql, $key = ' id ') { $this->rs= $this->query ($sql); $result =array (); while ($rows =mysql_fetch_array ($this->rs,mysql_assoc)) { $result [$rows [$key]]= $rows; } Mysql_free_result ($this->rs); Return Dircms_stripslashes ($this->filter_pass ($result)); } function last_insert_id () { if ($insertid =mysql_insert_id ($this->dblink)) >0 return $insertid; else//If the auto_increment column is of type bigint, the value returned by mysql_insert_id () will be incorrect. { $result = $this->fetch_one (' Select last_insert_id () as Insertid '); return $result [' Insertid ']; } } function Insert ($tbname, $varray, $replace =false) { $varray = $this->escape ($varray); $TB _fields= $this->get_fields ($tbname); Upgrade to determine whether a field exists foreach ($varray as $key = $value) { if (In_array ($key, $TB _fields)) { $fileds []= '. $key. ' `'; $values []=is_string ($value)? ". $value. ": $value; } } if ($fileds) { $fileds =implode (', ', $fileds); $fileds =str_replace ("', '", $fileds); $values =implode (', ', $values); $sql = $replace? " Replace into {$tbname} ({$fileds}) VALUES ({$values}) ': ' INSERT INTO {$tbname} ({$fileds}) VALUES ({$values}) "; $this->query ($sql, true); return $this->last_insert_id (); } else return false; } function Update ($tbname, $array, $where = ") { $array = $this->escape ($array); if ($where) { $TB _fields= $this->get_fields ($tbname); Increase the presence of the Judgment field $sql = "; foreach ($array as $k = $v) { if (In_array ($k, $TB _fields)) { $k =str_replace (' ', ' ', $k); $sql. = ", ' $k ' = ' $v '"; } } $sql = substr ($sql, 1); if ($sql) $sql = "Update" $tbname ' set $sql where $where "; else return true; } Else { $sql = "replace into ' $tbname ' ('". Implode ("," ", Array_keys ($array))." ') VALUES (' ". Implode (" ', ' ", $array)." ') "; } return $this->query ($sql, true); } function Mysql_delete ($tbname, $idarray, $filedname = ' id ') { $idwhere =is_array ($idarray)? Implode (', ', $idarray): Intval ($idarray); $where =is_array ($idarray)? " {$tbname}. {$filedname} in ({$idwhere}) ":" {$tbname}. {$filedname}={$idwhere} "; return $this->query ("delete from {$tbname} where {$where}", true); } function Get_fields ($table) { $fields =array (); $result = $this->fetch_all ("Show columns from ' {$table} '"); foreach ($result as $val) { $fields []= $val [' field ']; } return $fields; } function Get_table_status ($database) { $status =array (); $r = $this->fetch_all ("Show Table status from". $database. " `"); The Show table status is similar in nature to show table, but it can provide a lot of information for each table. foreach ($r as $v) { $status []= $v; } return $status; } function Get_one_table_status ($table) { return $this->fetch_one ("Show table status like ' $table '"); } function Create_fields ($tbname, $fieldname, $size =0, $type = ' varchar ')//2010-5-14 correction { if ($size) { $size =strtoupper ($type) = = ' varchar '? $size: 8; $this->query ("ALTER TABLE ' {$tbname} ' add ' $fieldname ' {$type} ({$size}) not NULL", true); } else $this->query ("ALTER TABLE ' {$tbname} ' add ' $fieldname ' mediumtext not NULL", true); return true; } function get_tables ()//Get all table table names { $tables =array (); $r = $this->fetch_all ("Show Tables"); foreach ($r as $v) { foreach ($v as $v _) { $tables []= $v _; } } return $tables; } function create_model_table ($tbname)//Create a Content model table (start: initial only field ContentID Int (20), for the table of contents,/////////////////////// update:2010-5-20 default added ' content ' mediumtext not null, field) { if (In_array ($tbname, $this->get_tables ())) return false; Returns false when the table name already exists if ($this->query ("CREATE table ' {$tbname} ' ( ' ContentID ' Mediumint (8) NOT NULL, ' content ' Mediumtext not NULL, Key (' ContentID ') Engine = MyISAM Default Charset=utf8 ", True)) return true; Returns true if successful return false; Failed to return false } function create_table ($tbname)//Create a member Model empty table (initial only field UserID Int (20), for membership table, 2010-4-26) { if (In_array ($tbname, $this->get_tables ())) return false; if ($this->query ("CREATE table ' {$tbname} ' ( ' UserID ' Mediumint (8) NOT NULL, Key (' UserID ') Engine = MyISAM Default Charset=utf8 ", True)) return true; return false; } function Escape ($STR)//filter Dangerous characters { if (!is_array ($STR)) return Str_replace (Array (' n ', ' R '), Array (CHR), CHR), mysql_real_escape_string (preg_ Replace ($this->search, $this->replace, $str), $this->dblink)); foreach ($str as $key = + $val) $str [$key] = $this->escape ($val); return $str; } function Filter_pass ($string, $allowedtags = ", $disabledattributes = Array (' onabort ', ' onactivate ', ' onafterprint ', ' O Nafterupdate ', ' onbeforeactivate ', ' onbeforecopy ', ' onbeforecut ', ' onbeforedeactivate ', ' onbeforeeditfocus ', ' Onbeforepaste ', ' onbeforeprint ', ' onbeforeunload ', ' onbeforeupdate ', ' onblur ', ' onbounce ', ' oncellchange ', ' onchange ' , ' onclick ', ' oncontextmenu ', ' oncontrolselect ', ' oncopy ', ' oncut ', ' ondataavaible ', ' ondatasetchanged ', ' Ondatasetcomplete ', ' ondblclick ', ' ondeactivate ', ' Ondrag ', ' ondragdrop ', ' ondragend ', ' ondragenter ', ' ondragleave ', ' OnDragOver ', ' ondragstart ', ' ondrop ', ' onerror ', ' onerrorupdate ', ' onfilterupdate ', ' onfinish ', ' onfocus ', ' Onfocusin ' , ' onfocusout ', ' onhelp ', ' onkeydown ', ' onkeypress ', ' onkeyup ', ' onlayoutcomplete ', ' onload ', ' onlosecapture ', ' OnMouseDown ', ' onmouseenter ', ' onmouseleave ', ' onmousemove ', ' onmoveout ', ' onmouseo tutorial ver ', ' onmouseup ', ' OnMouseWheel ', ' onmove ', ' onmoveend ', ' onmovestart ', ' onpaste ', ' onpropertychange ', ' Onreadys 'Tatechange ', ' onreset ', ' onresize ', ' onresizeend ', ' onresizestart ', ' onrowexit ', ' onrowsdelete ', ' onrowsinserted ', ' Onscroll ', ' onselect ', ' onselectionchange ', ' onselectstart ', ' onstart ', ' onstop ', ' onsubmit ', ' onunload ') { if (Is_array ($string)) { foreach ($string as $key = + $val) $string [$key] = $this->filter_pass ($val, allowed_htmltags); } Else { $string = Preg_replace ('/s (') implode (' | ', $disabledattributes). *? ([S>])/', ', Preg_replace ('/< (. *?) >/ie ', ' < '. Preg_replace ('/'/web effects: [^ ']*/i ', '/('. Implode (' | ', $disabledattributes). ") []*=[]*["'][^" ']*["']/i ', '/s+/'), Array (', ', '), Stripslashes (')). ' > ' ", Strip_tags ($string, $allowedtags))); } return $string; } function drop_table ($tbname) { return $this->query ("drop table if exists ' {$tbname} '", true); } Function version () { Return Mysql_get_server_info ($this->dblink); } }
I hope this article is helpful to everyone's PHP programming. http://www.bkjia.com/PHPjc/923906.html www.bkjia.com true http://www.bkjia.com/PHPjc/923906.html techarticle a simple and practical PHP operation MySQL database class, simple and practical MySQL This article describes a simple and practical PHP operation MySQL database class. Share to everyone for your reference. Specific as follows ... |