Posted By

browncardigan on 02/08/10


Tagged

mysql


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

tapdrup
eval


Class DB


 / Published in: PHP
 

URL: http://bettesmidler.com/code/2009-09-18.htm

Simple MySQL DB class.

  1. class Db {
  2.  
  3. private $_conf = false;
  4.  
  5. public function __construct() {
  6. $this->parseConf();
  7. $this->dbConnect();
  8. }
  9.  
  10. private function parseConf() {
  11. $this->_conf = parse_ini_file("crunksauce.ini", true);
  12. }
  13.  
  14. private function dbConnect() {
  15. if (!isset($this->_conf['database'])) {
  16. echo 'no db details found';
  17. }
  18. else {
  19. $db = $this->_conf['database'];
  20. $con = mysql_connect($db['host'], $db['user'], $db['pass'])
  21. or die("connect fail");
  22. mysql_select_db($db['dbname'], $con)
  23. or die("db select fail");
  24. }
  25. }
  26.  
  27. private function quote($str='') {
  28. return str_replace("'", "\'", $str);
  29. }
  30.  
  31. private function query($query=false) {
  32. return $query ? @mysql_query($query) : false;
  33. }
  34.  
  35. // public functions
  36.  
  37. public function result($query=false) {
  38. return $query ? @mysql_fetch_assoc($this->query($query)) : false;
  39. }
  40.  
  41. public function results($query=false) {
  42. $ar = false;
  43. if ($query) {
  44. $res = $this->query($query);
  45. if ($res) {
  46. $ar = array();
  47. while ($r = mysql_fetch_assoc($res)) {
  48. $ar[] = $r;
  49. }
  50. }
  51. }
  52. return $ar;
  53. }
  54.  
  55. public function insert($tbl=false, $data=array()) {
  56. if ($tbl && count($data) > 0) {
  57. foreach ($data as $key => $d) {
  58. if (!is_numeric($d)) {
  59. $data[$key] = "'" . $this->quote($d) . "'";
  60. }
  61. }
  62. $query = "INSERT INTO " . $tbl . "
  63. (" . implode(", ", array_keys($data)) . ")
  64. VALUES (" . implode(", ", $data) . ")";
  65. return $this->query($query) ? mysql_insert_id() : false;
  66. }
  67. else { return false; }
  68. }
  69.  
  70. public function update($tbl=false, $where=false, $data=array()) {
  71. if ($tbl && $where && count($data) > 0) {
  72. $set = array();
  73. foreach ($data as $key => $d) {
  74. $line = $key . "=";
  75. $line .= !is_numeric($d) ? "'" . $this->quote($d) . "'" : $d;
  76. $set[] = $line;
  77. }
  78. $query = "UPDATE " . $tbl . "
  79. SET " . implode(", ", $set) . "
  80. WHERE " . $where;
  81. return $this->query($query);
  82. }
  83. else { return false; }
  84. }
  85.  
  86. public function delete($tbl=false, $where=false) {
  87. if ($tbl && $where) {
  88. return $this->query("DELETE FROM " . $tbl . " WHERE " . $where);
  89. }
  90. else { return false; }
  91. }
  92.  
  93. public function debug($a) {
  94. echo "<pre>";
  95. print_r($a);
  96. echo "</pre>";
  97. }
  98.  
  99. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: daxumaming on December 8, 2010

too bad the code ain't documented.

You need to login to post a comment.