📜 ⬆️ ⬇️

We emulate PEAR DB when working with MySQL

At one time it became necessary to avoid using PEAR DB when working with MySQL. (due to obvious reasons: termination of support in recent versions of php, slowdown, etc.), but considering megatons of written code, I began to look for an emulator. To my surprise, I found nothing less suitable. Come to spend a little time and draw your own.
In the class I implemented only those parts that were used in projects (most likely in 99% of cases the rest is enough too), I think that it’s not difficult to add / correct to others. Since the projects for which this was done were completed long ago, this code did not receive further development. But if someone complements or corrects something, I will not mind.
The emulator consists of 2 parts, an error handling class with the ability to write them to the log and the emulator class PEAR DB inherited from it.
The code was successfully tested on 2 projects. Of course, in the code you can still fix and supplement a lot, but take it as a starting point from which you can move on. Additions and edits please in a personal, at their discretion, I will make them in the source code.
I hope someone will find this useful and please do not kick much, as everything that is done retroactively so that the code has already been passed for a long time suffers from its obvious flaws. Putting it here just want to help save someone time and not boast of its beauty.

Further in the case:

Error class:
<?php /* * Class: Error * Progr.: Mikhail Tchervonenko * Data: 2009-03-04 * EMail: rusmikleATgmailPointCom * ICQ: 35818796 * Skype: RusMikle */ class ERROR { var $show_errors =false; var $stop_after_error =false; var $error =0; var $error_message =""; var $error_messages =false; var $error_backtrase =false; var $error_filename = 'error_log/error_log.txt'; // *********************************** // ***** err_log function start function err_log($error_text="",$error_backtrase=false) { $this->error=1; $this->error_backtrase = $error_backtrase; $error_backtrase = $this->backtrace()."\n\r"; $this->error_message = "-----".date("DM j G:i:s T Y").$error_text; if (is_writable($this->error_filename)) { if (!$handle = fopen($this->error_filename, 'a')) { $ret = false; } if (fwrite($handle, "\n\r------\n\r".$this->error_message.$error_backtrase."\n\r-------\n\r") === FALSE) { $ret = false; } fclose($handle); $ret = true; } if($this->error_backtrase) $this->error_message .= "\n\r".$error_backtrase; $this->error_messages[] = $this->error_message; if($this->show_errors) echo str_replace("\n\r","<br>",$this->error_message); if($this->stop_after_error) exit(); return $ret; } // *********************************** // ***** backtrace function start function backtrace() { $output = "\n\r"; $output .= "Backtrace:\n\r"; $backtrace = debug_backtrace(); foreach ($backtrace as $bt) { $args = ''; foreach ($bt['args'] as $a) { if (!empty($args)) { $args .= ', '; } switch (gettype($a)) { case 'integer': case 'double': $args .= $a; break; case 'string': $a = substr($a, 0, 64).((strlen($a) > 64) ? '...' : ''); $args .= "\"$a\""; break; case 'array': $args .= 'Array('.count($a).')'; break; case 'object': $args .= 'Object('.get_class($a).')'; break; case 'resource': $args .= 'Resource('.strstr($a, '#').')'; break; case 'boolean': $args .= $a ? 'True' : 'False'; break; case 'NULL': $args .= 'Null'; break; default: $args .= 'Unknown'; } } $output .= "\n\r"; $output .= "file: {$bt['line']} - {$bt['file']}\n\r"; $output .= "call: {$bt['class']}{$bt['type']}{$bt['function']}($args)\n\r"; } $output .= "\n\r"; return $output; } // *********************************** // ***** getMessage function start function getMessage() { return $this->error_message; } } ?> 

')
And then the DB emulator itself

 <?php /* * Class: DB * Progr.: Mikhail Tchervonenko * Data: 2009-03-04 * EMail: rusmikleATgmailPointCom * ICQ: 35818796 * Skype: RusMikle */ class DB extends ERROR { var $tp ="cms1_"; var $conn =false; var $counter =0; // *********************************** // ***** set_database function start function set_database($database) { $res = mysql_select_db($database,$this->conn); if(!$res) { $this->err_log(); return false; } $this->error=0; return true; } // *********************************** // ***** construct function start function connect($host="localhost", $user="root", $pass="", $database="", $table_prefix="st__", $show_errors = false, $stop_after_error =false, $error_backtrase = false, $error_log_file="error_log/error_log.txt") { $this->tp = $table_prefix; $this->show_errors =$show_errors; $this->stop_after_error =$stop_after_error; $this->conn = mysql_connect($host, $user, $pass); if(!$this->conn) { $this->err_log(); return false; } if(!$this->set_database($database)) return false; $this->error=0; return true; } // *********************************** // ***** destruct function start function __destruct() { if(!empty($this->conn)) @mysql_close($this->conn); $this->conn = null; } // *********************************** // ***** close function start function close() { if(!empty($this->conn)) @$mysql_close($this->conn); $rs = null; $this->error=0; } // *********************************** // ***** query function start function query($query) { $this->error=0; $ret = mysql_query($query,$this->conn); if(!$ret) { $this->err_log(mysql_error()."\n\rQUERY:"."\n\r".$query); return false; } return $ret; } // *********************************** // ***** fcount function start function fcount($res) { if(!$res) return 0; $ret = @mysql_num_fields ($res); return $ret; } // *********************************** // ***** ecount function start function ecount($res) { if(!$res) return 0; $ret = @mysql_num_rows ($res); return $ret; } // *********************************** // ***** getOne function start function getOne($sql) { if(empty($sql)) { $this->error=1; return false; } $res = $this->query($sql); $ret = @mysql_fetch_array($res,MYSQL_NUM); if(!empty($res)) @mysql_free_result($res); return $ret[0]; } // *********************************** // ***** getRow function start function & getRow($sql, $ret_type=MYSQL_ASSOC) { // MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH if(empty($sql)) { $this->error=1; return false; } $this->counter = 0; $ret=array(); $res = $this->query($sql); $ret = @mysql_fetch_array($res, $ret_type); $this->counter = $this->ecount($res); if(!empty($res)) @mysql_free_result($res); return $ret; } // *********************************** // ***** getAll function start function & getAll($sql, $ret_type=MYSQL_ASSOC) { // MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH if(empty($sql)) { $this->error=1; return false; } $this->counter = 0; $ret=array(); $res = $this->query($sql); if(!empty($res)) $this->counter = $this->ecount($res); if($this->counter>0) { while ($row = @mysql_fetch_array($res, $ret_type)) $ret[] = $row; } if(!empty($res)) @mysql_free_result($res); return $ret; } // *********************************** // ***** nextId function start function nextId($table_name) { if(empty($table_name)) return false; $table_name=strtolower(trim($table_name))."_seq"; $this->query("CREATE TABLE IF NOT EXISTS `".$table_name."` (`id` bigint(20) unsigned) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); $this->query("LOCK TABLES ".$table_name." WRITE"); $id = $this->getOne("SELECT id FROM ".$table_name); if(empty($id)) { $id=1; $this->query("INSERT into ".$table_name." SET id=".$id); } else { $id++; $this->query("UPDATE ".$table_name." SET id=".$id); } $this->query("UNLOCK TABLES"); return $id; } // *********************************** // ***** iserror function start //        PEAR  !!!! function iserror($result) { if(!is_object($result) && !is_array($result) && empty($result)) return true; else return false; } }// End Class ?> 


I think further comments are unnecessary here, the code is not great and is quite simple. But if questions arise are ready to answer.

To start working with the class, it is enough to disable PEAR DB, enable this code, create an instance of the DB class and rewrite the connect function.

Sincerely, Mikhail Chervonenko.
ps Since the sources are not great, I allowed myself to post them entirely in the text of the article, again this will allow you to quickly fix if there are comments or suggestions.

Source: https://habr.com/ru/post/135654/


All Articles