Quick mysql in php


SUBMITTED BY: Guest

DATE: Jan. 15, 2014, 9:43 p.m.

FORMAT: PHP

SIZE: 3.7 kB

HITS: 787

  1. <?php
  2. //Written by: Shreyas Basarge
  3. define("db_server","localhost");
  4. define("db_user","root");
  5. define("db_password","root");
  6. class mysql
  7. {
  8. var $conn;
  9. var $error;
  10. function get_error() {
  11. return $this->error;
  12. }
  13. function connect($dbname) {
  14. $this->conn = mysql_connect(db_server, db_user, db_password);
  15. if (!!$this->conn)
  16. {
  17. mysql_select_db($dbname, $this->conn);
  18. return true;
  19. }
  20. $this->error = mysql_error();
  21. return false;
  22. }
  23. function insert($table, $dataarray)
  24. {
  25. $i = 0; $data = '';
  26. while(isset($dataarray[$i]))
  27. {
  28. $data .= "'" . $dataarray[$i] . "'";
  29. if (isset($dataarray[$i+1])) $data .= ", ";
  30. $i++;
  31. }
  32. if(mysql_query("INSERT INTO $table VALUES ($data)", $this->conn))
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. $this->error = mysql_error($this->conn);
  39. return false;
  40. }
  41. }
  42. function query($q)
  43. {
  44. if($result = mysql_query($q, $this->conn))
  45. {
  46. return $result;
  47. }
  48. else
  49. {
  50. $this->error=mysql_error($this->conn);
  51. return false;
  52. }
  53. }
  54. function update($table, $fields, $values, $clause)
  55. {
  56. if (count($fields) != count($values))
  57. {
  58. return false;
  59. }
  60. $sql = "UPDATE $table SET ";
  61. $i=0;
  62. while(isset($fields[$i]))
  63. {
  64. $sql = $sql . $fields[$i] . " = '" . $values[$i] . "'";
  65. if (isset($fields[$i])) $sql = $sql + ', ';
  66. $i++;
  67. }
  68. $sql = $sql + " WHERE $clause";
  69. return mysql_query($sql);
  70. }
  71. function max($table, $field)
  72. {
  73. $result = mysql_query("SELECT MAX($field) FROM $table", $this->conn);
  74. $row = mysql_fetch_array($result);
  75. return $row["MAX($field)"];
  76. }
  77. function getfields($table)
  78. {
  79. if($result = mysql_query("SHOW COLUMNS FROM $table", $this->conn))
  80. {
  81. $cms = mysql_fetch_assoc($result);
  82. return $cms['Field'];
  83. }
  84. else
  85. {
  86. $this->error = mysql_error($this->conn);
  87. return false;
  88. }
  89. }
  90. function getrows($table,$fields, $clause=NULL, $order=NULL, $llimit=NULL, $ulimit=NULL)
  91. {
  92. $sql = "SELECT ";
  93. $sql .= ($fields != '')? $fields . " " : "* ";
  94. $sql .= "FROM $table ";
  95. if (isset($clause)) $sql .= "WHERE $clause ";
  96. if ($order != '') $sql .= "ORDER BY $order ";
  97. if (isset($clause)) $sql .= "LIMIT $llimit, $ulimit";
  98. $sql .= ";";
  99. if($result = mysql_query($sql))
  100. {
  101. return $result;
  102. }
  103. else
  104. {
  105. $this->error=mysql_error($this->conn);
  106. return false;
  107. }
  108. }
  109. function getrow($table, $fields=NULL, $clause=NULL)
  110. {
  111. $rows = $this->getrows($table, $fields, $clause, '', 0, 1);
  112. if (mysql_num_rows($rows))
  113. return mysql_fetch_array($rows);
  114. else
  115. return false;
  116. }
  117. function nextrow($rows)
  118. {
  119. return mysql_fetch_array($rows);
  120. }
  121. function delete($table, $clause)
  122. {
  123. mysql_query("DELETE * FROM $table WHERE $clause");
  124. }
  125. function close()
  126. {
  127. mysql_close();
  128. }
  129. }
  130. ?>

comments powered by Disqus