MySQL Interface Class


/ Published in: PHP
Save to your folder(s)

Every MySQL class for PHP I've seen so far has been an epic disappointment. This will not lead you to feel the same way. Enjoy!


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // manages mysql connection
  4. class SQL {
  5.  
  6. private $conn, $lastq;
  7. public $query_count = 0;
  8.  
  9. // constructor. connects upon class formation.
  10. function SQL ($server, $user, $pw, $db, $persist) {
  11.  
  12. // connect
  13. $this->conn = (
  14. $persist ? @mysql_pconnect($server, $user, $pw) :
  15. @mysql_connect($server, $user, $pw))
  16. or exit ("Error Connecting to MySQL: ".mysql_error()."\n");
  17.  
  18. // select db
  19. @mysql_select_db($db, $this->conn) or
  20. exit ("Error Selecting MySQL DB: ".mysql_error() . "\n");
  21.  
  22. return true;
  23.  
  24. }
  25.  
  26. // run a query
  27. function query($q) {
  28. // run it
  29. $this->lastq = @mysql_query($q, $this->conn) or
  30. $this->error_message ("Query Error: ".mysql_error()."\n");
  31.  
  32. // increment query count
  33. $this->query_count++;
  34.  
  35. // return result
  36. return $this->lastq;
  37. }
  38.  
  39. // run an unbuffered query. (used only a few times)
  40. function query_unbuff($q)
  41. {
  42. // run it
  43. $this->lastq = @mysql_unbuffered_query($q, $this->conn) or
  44. $this->error_message ("Query Error: ".mysql_error()."\n");
  45.  
  46. // increment query count
  47. $this->query_count++;
  48.  
  49. // return result
  50. return $this->lastq;
  51.  
  52. }
  53.  
  54. // free a result
  55. function free($r) {
  56. }
  57.  
  58. // free last query result
  59. function freelast() {
  60. if (is_resource($this->conn) && is_resource($this->lastq))
  61. @mysql_free_result($this->lastq);
  62. }
  63.  
  64. // get result as a numerical array
  65. function fetch_row($r = null) {
  66. $r = @$r ? $r : $this->lastq;
  67. return @mysql_fetch_row($r);
  68. }
  69.  
  70. // get result as a associative array
  71. function fetch_assoc($r = null) {
  72. $r = @$r ? $r : $this->lastq;
  73. return @mysql_fetch_assoc($r);
  74. }
  75.  
  76. // close mysql connection
  77. function close() {
  78. if (is_resource($this->conn))
  79. mysql_close($this->conn);
  80. }
  81.  
  82. // escape
  83. function prot($s) {
  84. return ctype_digit($s) ? $s : mysql_real_escape_string($s, $this->conn);
  85. }
  86.  
  87. // last id
  88. function lastid() {
  89. return mysql_insert_id($this->conn);
  90. }
  91.  
  92. // rows affected by last update/replace/delete/insert
  93. function affected_rows() {
  94. return mysql_affected_rows($this->conn);
  95. }
  96.  
  97. // show error message
  98. function error_message() {
  99.  
  100. // halt whilst showing the message
  101. exit ("MySQL Error: \n".mysql_error($this->conn)."\n");
  102.  
  103. }
  104.  
  105. // are we connected?
  106. function is_connected() {
  107. return is_resource($this->conn);
  108. }
  109.  
  110. // return number of results
  111. function num($r) {
  112. return mysql_num_rows($r);
  113. }
  114. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.