Quick mysql in php


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

Write less and do more mysql in php.


Copy this code and paste it in your HTML
  1. <?php
  2. //Written by: Shreyas Basarge
  3.  
  4.  
  5. define("db_server","localhost");
  6.  
  7. define("db_user","root");
  8.  
  9. define("db_password","root");
  10.  
  11.  
  12.  
  13. class mysql
  14.  
  15. {
  16.  
  17.  
  18.  
  19. var $conn;
  20.  
  21. var $error;
  22.  
  23.  
  24.  
  25. function get_error() {
  26.  
  27. return $this->error;
  28.  
  29. }
  30.  
  31.  
  32.  
  33. function connect($dbname) {
  34.  
  35. $this->conn = mysql_connect(db_server, db_user, db_password);
  36.  
  37. if (!!$this->conn)
  38.  
  39. {
  40.  
  41. mysql_select_db($dbname, $this->conn);
  42.  
  43.  
  44. return true;
  45.  
  46. }
  47.  
  48.  
  49.  
  50. $this->error = mysql_error();
  51.  
  52. return false;
  53.  
  54. }
  55.  
  56.  
  57.  
  58. function insert($table, $dataarray)
  59.  
  60. {
  61.  
  62. $i = 0; $data = '';
  63.  
  64. while(isset($dataarray[$i]))
  65.  
  66. {
  67.  
  68. $data .= "'" . $dataarray[$i] . "'";
  69.  
  70. if (isset($dataarray[$i+1])) $data .= ", ";
  71.  
  72. $i++;
  73.  
  74. }
  75.  
  76. if(mysql_query("INSERT INTO $table VALUES ($data)", $this->conn))
  77.  
  78. {
  79.  
  80. return true;
  81.  
  82. }
  83.  
  84. else
  85.  
  86. {
  87.  
  88. $this->error = mysql_error($this->conn);
  89.  
  90. return false;
  91.  
  92. }
  93.  
  94. }
  95.  
  96.  
  97.  
  98. function query($q)
  99.  
  100. {
  101.  
  102. if($result = mysql_query($q, $this->conn))
  103.  
  104. {
  105.  
  106. return $result;
  107.  
  108. }
  109.  
  110. else
  111.  
  112. {
  113.  
  114. $this->error=mysql_error($this->conn);
  115.  
  116. return false;
  117.  
  118. }
  119.  
  120. }
  121.  
  122.  
  123.  
  124. function update($table, $fields, $values, $clause)
  125.  
  126. {
  127.  
  128. if (count($fields) != count($values))
  129.  
  130. {
  131.  
  132. return false;
  133.  
  134. }
  135.  
  136. $sql = "UPDATE $table SET ";
  137.  
  138. $i=0;
  139.  
  140. while(isset($fields[$i]))
  141.  
  142. {
  143.  
  144. $sql = $sql . $fields[$i] . " = '" . $values[$i] . "'";
  145.  
  146. if (isset($fields[$i])) $sql = $sql + ', ';
  147.  
  148. $i++;
  149.  
  150. }
  151.  
  152. $sql = $sql + " WHERE $clause";
  153.  
  154.  
  155.  
  156. return mysql_query($sql);
  157.  
  158.  
  159.  
  160. }
  161.  
  162.  
  163.  
  164. function max($table, $field)
  165.  
  166. {
  167.  
  168. $result = mysql_query("SELECT MAX($field) FROM $table", $this->conn);
  169.  
  170. $row = mysql_fetch_array($result);
  171.  
  172. return $row["MAX($field)"];
  173.  
  174. }
  175.  
  176.  
  177.  
  178. function getfields($table)
  179.  
  180. {
  181.  
  182. if($result = mysql_query("SHOW COLUMNS FROM $table", $this->conn))
  183.  
  184. {
  185.  
  186. $cms = mysql_fetch_assoc($result);
  187.  
  188. return $cms['Field'];
  189.  
  190. }
  191.  
  192. else
  193.  
  194. {
  195.  
  196. $this->error = mysql_error($this->conn);
  197.  
  198. return false;
  199.  
  200. }
  201.  
  202. }
  203.  
  204.  
  205.  
  206. function getrows($table,$fields, $clause=NULL, $order=NULL, $llimit=NULL, $ulimit=NULL)
  207.  
  208. {
  209.  
  210. $sql = "SELECT ";
  211.  
  212. $sql .= ($fields != '')? $fields . " " : "* ";
  213.  
  214. $sql .= "FROM $table ";
  215.  
  216. if (isset($clause)) $sql .= "WHERE $clause ";
  217.  
  218. if ($order != '') $sql .= "ORDER BY $order ";
  219.  
  220. if (isset($clause)) $sql .= "LIMIT $llimit, $ulimit";
  221.  
  222. $sql .= ";";
  223.  
  224.  
  225.  
  226.  
  227. if($result = mysql_query($sql))
  228.  
  229. {
  230.  
  231. return $result;
  232.  
  233. }
  234.  
  235. else
  236.  
  237. {
  238.  
  239. $this->error=mysql_error($this->conn);
  240.  
  241. return false;
  242.  
  243. }
  244.  
  245. }
  246.  
  247.  
  248.  
  249. function getrow($table, $fields=NULL, $clause=NULL)
  250.  
  251. {
  252.  
  253. $rows = $this->getrows($table, $fields, $clause, '', 0, 1);
  254.  
  255.  
  256.  
  257. if (mysql_num_rows($rows))
  258.  
  259. return mysql_fetch_array($rows);
  260.  
  261. else
  262.  
  263. return false;
  264.  
  265.  
  266.  
  267. }
  268.  
  269.  
  270.  
  271. function nextrow($rows)
  272.  
  273. {
  274.  
  275. return mysql_fetch_array($rows);
  276.  
  277. }
  278.  
  279.  
  280.  
  281. function delete($table, $clause)
  282.  
  283. {
  284.  
  285. mysql_query("DELETE * FROM $table WHERE $clause");
  286.  
  287. }
  288.  
  289.  
  290.  
  291. function close()
  292.  
  293. {
  294.  
  295.  
  296. }
  297.  
  298.  
  299.  
  300. }
  301.  
  302. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.