Updated PHP database class


SUBMITTED BY: Guest

DATE: June 16, 2015, 10:36 p.m.

FORMAT: Text only

SIZE: 1.7 kB

HITS: 544

  1. <?php // mysqli.php - contains the MySQL class
  2. // define db class
  3. class db_class {
  4. var $dbc;
  5. var $username;
  6. var $password;
  7. var $host;
  8. var $db_name;
  9. var $results = array();
  10. var $count;
  11. function __construct($username, $password, $host, $db) {
  12. // connect to the database
  13. $this->username = $username;
  14. $this->password = $password;
  15. $this->host = $host;
  16. $this->db_name = $db;
  17. try {
  18. $this->dbc = mysqli_connect($this->host, $this->username, $this->password, $this->db_name);
  19. // set encoding
  20. mysqli_set_charset($this->dbc, 'utf8');
  21. } catch(Exception $e) {
  22. echo "ERROR - Failed to open stream to database";
  23. }
  24. }
  25. function query($query) {
  26. try {
  27. $r = mysqli_query($this->dbc, $query);
  28. $this->count++;
  29. return $r;
  30. }
  31. catch(Exception $e) {
  32. echo "There was a problem with the database, We apologize for the inconvenience ".$e->getMessage();
  33. }
  34. }
  35. function fetchresult($query) {
  36. $r = $this->query($query); // get the result(s) of an executed query.
  37. try {
  38. if(mysqli_num_rows($r) >= 1) { // get the result
  39. $row = mysqli_fetch_array($r);
  40. $this->results[] = $row;
  41. return $row;
  42. } else {
  43. return false;
  44. }
  45. } catch(Exception $e) {
  46. echo "<b>There was an error in processing your request. We are sorry for the inconvenience.";
  47. }
  48. }
  49. function numrows($query) {
  50. $r = $this->query($query);
  51. return mysqli_num_rows($r);
  52. }
  53. function escape_data($data) {
  54. return trim(mysqli_real_escape_string($this->dbc, $data));
  55. }
  56. }
  57. ?>

comments powered by Disqus