Guest User

Untitled

a guest
Jul 21st, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. <?php
  2.  
  3. //DB.class.php
  4.  
  5. class DB {
  6.  
  7. protected $db_name = 'tidbit';
  8. protected $db_user = 'root';
  9. protected $db_pass = 'jorp12';
  10. protected $db_host = 'localhost';
  11.  
  12. // Open a connection to the database. Make sure this is called
  13. // on every page that needs to use the database.
  14. public function connect() {
  15. $connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
  16. mysql_select_db($this->db_name);
  17.  
  18. return true;
  19. }
  20.  
  21. // Takes a MySQL row set and returns an associative array, where the keys
  22. // in the array are the column names in the row set. If singleRow is set to
  23. // true, then it will return a single row instead of an array of rows.
  24. public function processRowSet($rowSet, $singleRow=false)
  25. {
  26. $resultArray = array();
  27. while($row = mysql_fetch_assoc($rowSet))
  28. {
  29. array_push($resultArray, $row);
  30. }
  31.  
  32. if ($singleRow === true)
  33. return $resultArray[0];
  34.  
  35. return $resultArray;
  36. }
  37.  
  38. // Select rows from the database.
  39. // Returns a full row or rows from $table using $where as the where clause.
  40. // Return value is an associative array with column names as keys.
  41. public function select($table, $where) {
  42. $sql = "SELECT * FROM $table WHERE $where";
  43. $result = mysql_query($where);
  44. if(mysql_num_rows($result) == 1)
  45. return $this->processRowSet($result, true);
  46.  
  47. return $this->processRowSet($result);
  48. }
  49.  
  50. // Updates a current row in the database.
  51. // Takes an array of data, where the keys in the array are the column names
  52. // and the values are the data that will be inserted into those columns.
  53. // $table is the name of the table and $where is the sql where clause.
  54. public function update($data, $table, $where) {
  55. foreach ($data as $column => $value) {
  56. $sql = "UPDATE $table SET $column = $value WHERE $where";
  57. mysql_query($sql) or die(mysql_error());
  58. }
  59.  
  60. return true;
  61. }
  62.  
  63. // Inserts a new row into the database.
  64. // Takes an array of data, where the keys in the array are the column names
  65. // and the values are the data that will be inserted into those columns.
  66. // $table is the name of the table.
  67. public function insert($data, $table) {
  68.  
  69. $columns = "";
  70. $values = "";
  71.  
  72. foreach ($data as $column => $value) {
  73. $columns .= ($columns == "") ? "" : ", ";
  74. $columns .= $column;
  75. $values .= ($values == "") ? "" : ", ";
  76. $values .= $value;
  77. }
  78.  
  79. $sql = "insert into $table ($columns) values ($values)";
  80.  
  81. mysql_query($sql) or die(mysql_error());
  82.  
  83. // Return the ID of the user in the database.
  84. return mysql_insert_id();
  85.  
  86. }
  87.  
  88. }
  89.  
  90. ?>
Add Comment
Please, Sign In to add comment