PHP MySQL API


SUBMITTED BY: Guest

DATE: May 11, 2015, 2:55 p.m.

FORMAT: PHP

SIZE: 1.7 kB

HITS: 1504

  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($r) { // get the result(s) of an executed query.
  36. try {
  37. if(mysqli_num_rows($r) >= 1) { // get the result
  38. $row = mysqli_fetch_array($r);
  39. $this->results[] = $row;
  40. return $row;
  41. } else {
  42. return false;
  43. }
  44. } catch(Exception $e) {
  45. echo "<b>There was an error in processing your request. We are sorry for the inconvenience.";
  46. }
  47. }
  48. function numrows($query) {
  49. $r = $this->query($query);
  50. return mysqli_num_rows($r);
  51. }
  52. function escape_data($data) {
  53. return trim(mysqli_real_escape_string($this->dbc, $data));
  54. }
  55. }
  56. ?>

comments powered by Disqus