Guest User

Untitled

a guest
Aug 5th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. <?php
  2. class database {
  3. private $conn;
  4. var $host = "localhost";
  5. var $db = "my_table";
  6. var $user = "root";
  7. var $pass = "";
  8.  
  9. // Connect to the Database
  10. function __construct() {
  11. $this->conn = new PDO("mysql:host=$this->host;dbname=$this->db",$this->user,$this->pass);
  12. }
  13.  
  14. // Prepare and Run a Query with bound or non bound parameters
  15. function query($query, $valsArray='') {
  16. $stmt = $this->conn->prepare($query);
  17. if(!$stmt) { print_r($this->conn->errorInfo()); }
  18. elseif ($valsArray) {
  19. foreach ($valsArray as $key => $val) {
  20. if (is_int($val)) { $stmt->bindValue($key, $val, PDO::PARAM_INT); }
  21. else { $stmt->bindValue($key, $val, PDO::PARAM_STR); }
  22. }
  23. if(!$stmt->execute()) { print_r($stmt->errorInfo()); }
  24. }
  25. else {
  26. if(!$stmt->execute()) { print_r($stmt->errorInfo()); }
  27. }
  28. return $stmt;
  29. }
  30.  
  31. // Count Number of Rows Function
  32. // Query needs to have this syntax "SELECT SQL_CALC_FOUND_ROWS columns...."
  33. function count_rows() {
  34. $table_rows = $this->conn->query('SELECT FOUND_ROWS()');
  35. $count = $table_rows->fetchColumn();
  36. return $count;
  37. }
  38.  
  39. // Create a UUID
  40. function uuid() {
  41. $stmt = $this->conn->query("SELECT UUID()");
  42. $row = $stmt->fetch(PDO::FETCH_NUM);
  43. return $row[0];
  44. }
  45. }
  46. ?>
Add Comment
Please, Sign In to add comment