The database connection and text cache are encapsulated in a class.
- Class Db {
- Protected $ _ connect;
- Protected $ _ db = Array ();
- Protected $ _ cache = Array ();
- Public function _ construct ($ args ){
- List ($ this-> _ db, $ this-> _ cache) = $ args;
- }
- Protected function connect ($ db ){
- $ This-> _ connect = mysql_connect ($ db ['hostname'], $ db ['username'], $ db ['password']);
- Mysql_set_charset ('utf8 ');
- Mysql_select_db ($ db ['databasename '], $ this-> _ connect );
- }
- /**
- * Function: obtain the table data and format the queried data. the returned format is an array!
- * $ SQL: the input SQL statement to be executed, which must be SELECT
- *
- */
- Public function fetch ($ SQL ){
- $ Result = '';
- If (isset ($ this-> _ cache ['expire ']) {
- $ Name = md5 (strtolower (str_replace ('','', $ SQL )));
- $ Dir = substr ($ name, 0, 2 );
- $ Dir = $ this-> _ cache ['dir']. '/'. $ dir;
- ! Is_dir ($ dir) & mkdir ($ dir, 0777 );
- ! Is_dir ($ dir) & mkdir ($ dir, 0777 );
- $ This-> _ cache ['path'] = $ dir. '/'. $ name;
- If (is_file ($ this-> _ cache ['path']) & $ this-> check_expire ()){
- $ Result = $ this-> get ();
- }
- }
- If ($ result = ''){
- $ Data = $ this-> exec ($ SQL );
- $ Result = Array ();
- While ($ result [] = mysql_fetch_array ($ data, MYSQL_ASSOC) {}// remove the index
- Mysql_free_result ($ data );
- Array_pop ($ result );
- Isset ($ this-> _ cache ['expire ']) & $ this-> write ($ result );
- }
- Return $ result;
- }
- /**
- * Function: execute all SQL statements, but not SELECT!
- * $ SQL: the input SQL statement to be executed. it cannot be a SELECT statement.
- * Return value: TRUE OR FALSE
- */
- Public function exec ($ SQL ){
- If ($ this-> _ connect === null) $ this-> connect ($ this-> _ db); // data link
- If ($ result = mysql_query ($ SQL, $ this-> _ connect )){
- Return $ result;
- } Else {
- Die ("{$ SQL}
Execution error: ". mysql_error ());
- }
- }
- /**
- * Function: execute the database INSERT statement. it can only be an INSERT statement!
- * $ V: The input condition to be executed. in the array format, table indicates the table to be inserted, row indicates the field, and value indicates the value to be inserted.
- * Return value: mysql_insert_id () OR FALSE
- */
- Public function insert ($ table, $ field, $ ignore = 0 ){
- $ D = Array ('field' => '', 'Val' => '');
- Foreach ($ field AS $ key => $ v ){
- $ D ['field']. = $ key .',';
- $ D ['Val']. = "'{$ this-> escape ($ v )}',";
- }
- $ D ['field'] = rtrim ($ D ['field'], ',');
- $ D ['Val'] = rtrim ($ D ['Val'], ',');
- $ Ignore = $ ignore> 0? 'Ignore ':'';
- $ SQL = "INSERT {$ ignore} INTO {$ this-> _ db ['perfix']} {$ table} ({$ D ['field']}) VALUES ({$ D ['Val']}) ";
- If ($ this-> exec ($ SQL )){
- $ Insert_id = mysql_insert_id ();
- Return is_numeric ($ insert_id )? $ Insert_id: TRUE;
- } Else {
- Return FALSE;
- }
- }
- Public function update ($ table, $ field ){
- $ D = Array ('where' => '', 'str' => '');
- $ Index = 0;
- Foreach ($ field AS $ key => $ v ){
- $ Index = 0? $ D ['where'] = "{$ key} = '{$ this-> escape ($ v)}'": $ D ['str']. = "{$ key} = '{$ this-> escape ($ v )}',";
- $ Index ++;
- }
- $ D ['str'] = rtrim ($ D ['str'], ',');
- $ SQL = "UPDATE {$ this-> _ db ['perfix']} {$ table} SET {$ D ['str']} WHERE {$ D ['where' ]} ";
- Return $ this-> exec ($ SQL );
- }
- Public function delete ($ table, $ field ){
- $ Str = '';
- Foreach ($ field AS $ key => $ v ){
- $ Str = "{$ key} = '{$ v }'";
- }
- $ SQL = 'delete from'. $ this-> _ db ['perfix']. $ table. 'where'. $ str. 'limit 1 ';
- Return $ this-> exec ($ SQL );
- }
- Public function sum ($ table, $ condition ){
- $ Totle = $ this-> fetch ('SELECT COUNT (*) AS totle FROM '. $ this-> _ db ['perfix']. $ table. 'Where '. $ condition );
- Return $ totle [0] ['totle '];
- }
- /**
- * Function: filter out special characters.
- * $ V: parameters to be passed in for detection
- * Return value: the detected parameter.
- */
- Public function escape ($ v ){
- Return mysql_real_escape_string ($ v );
- }
- /*
- * Function: determines the cache.
- */
- Public function cache ($ name, $ expire = 100000000 ){
- $ This-> _ cache ['expire '] = $ expire;
- Return $ this;
- }
- Public function check_expire (){
- Return (filemtime ($ this-> _ cache ['path']) + $ this-> _ cache ['expire '])> strtotime ("now ");
- }
- Public function write ($ data ){
- $ F = fopen ($ this-> _ cache ['path'], 'w ');
- If ($ f ){
- Flock ($ f, LOCK_EX );
- Fseek ($ f, 0 );
- Ftruncate ($ f, 0 );
- $ Tmp = fwrite ($ f, serialize ($ data ));
- If (! ($ Tmp = false )){
- $ Result = true;
- }
- Fclose ($ f );
- }
- Chmod ($ this-> _ cache ['path'], 0777 );
- }
- Public function get (){
- $ F = fopen ($ this-> _ cache ['path'], 'r ');
- $ Data = fread ($ f, filesize ($ this-> _ cache ['path']);
- Fclose ($ f );
- Return unserialize ($ data );
- }
- Public function delete_dir ($ dir = ''){
- $ Dir = empty ($ dir )? $ This-> _ cache ['dir']: $ dir;
- ! Is_dir ($ dir) & exit;
- $ D = opendir ($ dir );
- $ I = 0;
- While ($ file = readdir ($ d ))! = False ){
- $ Path = $ dir. '/'. $ file;
- If ($ I> 1) is_file ($ path )? Unlink ($ path): $ this-> delete_dir ($ path );
- $ I ++;
- }
- Closedir ($ d );
- Rmdir ($ dir );
- }
- Public function _ destruct (){
- Isset ($ this-> _ connect) & mysql_close ($ this-> _ connect );
- }
- }
|