Guest User

Untitled

a guest
Jun 27th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2.  
  3. class DBController
  4. {
  5.  
  6. private $host = “localhost”;
  7.  
  8. private $user = “root”;
  9.  
  10. private $password = “”;
  11.  
  12. private $database = “shopping_cart”;
  13.  
  14. private static $conn;
  15.  
  16. function __construct()
  17. {
  18. $this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->database);
  19. }
  20.  
  21. public static function getConnection()
  22. {
  23. if (empty($this->conn)) {
  24. new Database();
  25. }
  26. }
  27.  
  28. function getDBResult($query, $params = array())
  29. {
  30. $sql_statement = $this->conn->prepare($query);
  31. if (! empty($params)) {
  32. $this->bindParams($sql_statement, $params);
  33. }
  34. $sql_statement->execute();
  35. $result = $sql_statement->get_result();
  36.  
  37. if ($result->num_rows > 0) {
  38. while ($row = $result->fetch_assoc()) {
  39. $resultset[] = $row;
  40. }
  41. }
  42.  
  43. if (! empty($resultset)) {
  44. return $resultset;
  45. }
  46. }
  47.  
  48. function updateDB($query, $params = array())
  49. {
  50. $sql_statement = $this->conn->prepare($query);
  51. if (! empty($params)) {
  52. $this->bindParams($sql_statement, $params);
  53. }
  54. $sql_statement->execute();
  55. }
  56.  
  57. function bindParams($sql_statement, $params)
  58. {
  59. $param_type = “”;
  60. foreach ($params as $query_param) {
  61. $param_type .= $query_param[“param_type”];
  62. }
  63.  
  64. $bind_params[] = & $param_type;
  65. foreach ($params as $k => $query_param) {
  66. $bind_params[] = & $params[$k][“param_value”];
  67. }
  68.  
  69. call_user_func_array(array(
  70. $sql_statement,
  71. ‘bind_param’
  72. ), $bind_params);
  73. }
  74. }
Add Comment
Please, Sign In to add comment