mysql/sphinx database class


SUBMITTED BY: phpsnippets

DATE: Oct. 21, 2015, 2:01 p.m.

FORMAT: Text only

SIZE: 5.5 kB

HITS: 1673

  1. <?php
  2. /*
  3. Methods:
  4. connect
  5. Connectects to a mysql or sphinx database
  6. query
  7. queries the database and saves the result to the class
  8. q
  9. queries the database but does NOT save the result to the class
  10. insertID
  11. gets the insert ID from the query when using an insert
  12. getRow
  13. gets 1 row from the database and returns it as an array
  14. row
  15. gets the current row from a result set
  16. numRows
  17. gets the number of returned rows
  18. affectedRows
  19. gets the number of rows affected by an insert, update, delete
  20. field
  21. gets a field from a result set
  22. getOne
  23. gets one item from a database row
  24. escape
  25. escapes your query
  26. */
  27. class Mysql{
  28. private $conn = null;
  29. private $sql = null;
  30. private $row = null;
  31. private $server = "", $database;
  32. private $sphinx = false;
  33. public function __construct($host = null, $user = null, $pass = null, $db = null, $port = 3306, $sphinx = false){
  34. if(is_string($host) && is_string($user) && is_string($pass) && is_string($db)){
  35. $this=>connect($host, $user, $pass, $db, $port, $sphinx);
  36. }
  37. }
  38. public function __destruct(){
  39. mysql_close($this->conn);
  40. }
  41. public function connect($host, $user, $pass, $db, $port = 3306, $sphinx = false){
  42. $this->sphinx = (bool)$sphinx;
  43. $this->conn = @mysql_connect("$host:$port", "$user", "$pass");
  44. mysql_select_db($db, $this->conn);
  45. if(!$this->sphinx){
  46. $this->query("set time_zone = 'UTC'");
  47. }
  48. return $this;
  49. }
  50. public function query($query){
  51. if($this->conn === null)
  52. return false;
  53. $this->sql = mysql_query($query, $this->conn);
  54. if(mysql_error()){
  55. echo mysql_error();
  56. }
  57. return $this;
  58. }
  59. public function q($qurey){
  60. if($this->conn === null)
  61. return false;
  62. mysql_query($qurey, $this->conn);
  63. if(mysql_error()){
  64. echo mysql_error();
  65. }
  66. return $this;
  67. }
  68. public function insertID(){
  69. return mysql_insert_id($this->conn);
  70. }
  71. public function getRow($query){
  72. if($this->conn === null)
  73. return false;
  74. $this->sql = mysql_query($query, $this->conn);
  75. return mysql_fetch_assoc($this->sql);
  76. }
  77. public function row(){
  78. $this->row = mysql_fetch_assoc($this->sql);
  79. return $this->row;
  80. }
  81. public function numRows(){
  82. return mysql_num_rows($this->sql);
  83. }
  84. public function affectedRows(){
  85. return mysql_affected_rows($this->conn);
  86. }
  87. public function field($name, $default = ""){
  88. if(isset($this->row[$name]))
  89. return $this->row[$name];
  90. return $default;
  91. }
  92. public function getOne($query, $default = ""){
  93. if($this->conn === null)
  94. return false;
  95. $sql = mysql_query($query, $this->conn);
  96. $row = mysql_fetch_array($sql);
  97. if(isset($row[0]))
  98. return $row[0];
  99. return $default;
  100. }
  101. public function escape($string){
  102. if($this->conn === null)
  103. return false;
  104. return mysql_real_escape_string($string, $this->conn);
  105. }
  106. public function calcFoundRows(){
  107. if($this->sphinx){
  108. $sql = mysql_query("show meta", $this->conn);
  109. $row = mysql_fetch_assoc($sql);
  110. mysql_data_seek($sql, 1);
  111. return $row['Value'];
  112. }else{
  113. $sql = mysql_query("select found_rows() as total", $this->conn);
  114. $row = mysql_fetch_assoc($sql);
  115. return $row['total'];
  116. }
  117. }
  118. }
  119. ?>
  120. Usage
  121. 1
  122. 2
  123. 3
  124. 4
  125. 5
  126. 6
  127. 7
  128. 8
  129. 9
  130. 10
  131. 11
  132. 12
  133. 13
  134. 14
  135. 15
  136. 16
  137. 17
  138. 18
  139. 19
  140. 20
  141. 21
  142. 22
  143. 23
  144. 24
  145. 25
  146. 26
  147. 27
  148. 28
  149. 29
  150. 30
  151. 31
  152. 32
  153. 33
  154. 34
  155. 35
  156. 36
  157. 37
  158. 38
  159. 39
  160. 40
  161. 41
  162. 42
  163. <?php
  164. $db = new Mysql("localhost", "user", "password", "database");
  165. // Looping through a result set:
  166. $db->query("select * from mytable limit 10");
  167. // You can assign row() to a variable if you want
  168. // while($row = $db->row()){
  169. // $user = $row["user"];
  170. // $email = $row["email"];
  171. // echo "$user:$email\n";
  172. // }
  173. while($db->row()){
  174. $user = $db->field("user");
  175. $email = $db->field("email");
  176. echo "$user:$email\n";
  177. }
  178. // getting one row
  179. $row = $db->getRow("select * from users where userid = 123");
  180. $user = $row["user"];
  181. $email = $row["email"];
  182. echo "$user:$email\n";
  183. // getting one item
  184. // Example 1
  185. $user = $db->getOne("select user from users where userid = 123");
  186. echo $user;
  187. // Example 2
  188. $user = $db->getOne("select user from users where userid = 123", "Admin User");
  189. echo $user; // Displays "Admin User" if userid 123 was not found
  190. // Example 3
  191. $is_user = (bool)$db->getOne("select 1 from users where userid = 123");
  192. if($is_user){
  193. echo "User exists";
  194. }else{
  195. echo "User does not exists";
  196. }
  197. // Insert example
  198. $user = $db->escape($_POST["user"]);
  199. $email = $db->escape($_POST["email"]);
  200. $db->query("insert into mytable (user, email) values ('$user', '$email')");
  201. $insert_id = $db->insertID();
  202. echo "$insert_id\n";
  203. ?>

comments powered by Disqus