database.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?
  2. define ("server" , $server );
  3. define ("db_user" , $db_user);
  4. define ("db_pass" , $db_pass);
  5. define ("only_db" , $only_db);
  6. class DB {
  7. var $srv = server;
  8. var $usr = db_user;
  9. var $pwd = db_pass;
  10. var $odb = only_db;
  11. var $pcn = false;
  12. var $doe = true;
  13. var $fel = true;
  14. var $error_log = "mysql_errors.log";
  15. function connect(){
  16. if (!$this->link){
  17. $this->link = ($this->pcn) ? mysql_pconnect($this->srv, $this->usr, $this->pwd) :
  18. mysql_connect( $this->srv, $this->usr, $this->pwd) or
  19. $this->error();
  20. }
  21. if (!mysql_select_db($this->odb,$this->link)) $this->error();
  22. if($this->link) mysql_query("SET NAMES 'utf8'");
  23. return ($this->link) ? true : false;
  24. }
  25. function query($sql){
  26. if (!$this->link && !$this->connect()) $this->error();
  27. if (!($this->result = mysql_query($sql, $this->link))) $this->error($sql);
  28. return ($this->result) ? true : false;
  29. }
  30. function nextr(){
  31. if (!$this->result) die ("No query pending");
  32. unset($this->row);
  33. $this->row = mysql_fetch_array($this->result, MYSQL_BOTH);
  34. return ($this->row) ? true : false ;
  35. }
  36. function f($index){
  37. return stripslashes($this->row[$index]);
  38. }
  39. function goto($row){
  40. if (!$this->result) die ("No query pending");
  41. if (!mysql_data_seek($this->result, $row)) $this->error();
  42. }
  43. function nf(){
  44. if ($numb = mysql_num_rows($this->result) === false) $this->error();
  45. return mysql_num_rows($this->result);
  46. }
  47. function af(){
  48. return mysql_affected_rows();
  49. }
  50. function error($string=""){
  51. echo $error = mysql_error();
  52. if ($this->fel){
  53. $fp = fopen($this->error_log,"a+");
  54. fwrite($fp, "[".date("Y-m-d H:i:s")."]<".$error.">$string\n");
  55. fclose($fp);
  56. }
  57. if ($this->doe){
  58. if (isset($this->result)) mysql_free_result($this->result);
  59. mysql_close($this->link);
  60. die();
  61. }
  62. }
  63. function insert_id(){
  64. if(!$this->link) return false;
  65. return mysql_insert_id();
  66. }
  67. function escape($string){
  68. if(!$this->link) return addslashes($string);
  69. return mysql_real_escape_string($string);
  70. }
  71. function destroy(){
  72. if (isset($this->result)) mysql_free_result($this->result);
  73. if (isset($this->link_id)) mysql_close($this->link_id);
  74. }
  75. }
  76. ?>