Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class database {
- private $conn;
- var $host = "localhost";
- var $db = "my_table";
- var $user = "root";
- var $pass = "";
- // Connect to the Database
- function __construct() {
- $this->conn = new PDO("mysql:host=$this->host;dbname=$this->db",$this->user,$this->pass);
- }
- // Prepare and Run a Query with bound or non bound parameters
- function query($query, $valsArray='') {
- $stmt = $this->conn->prepare($query);
- if(!$stmt) { print_r($this->conn->errorInfo()); }
- elseif ($valsArray) {
- foreach ($valsArray as $key => $val) {
- if (is_int($val)) { $stmt->bindValue($key, $val, PDO::PARAM_INT); }
- else { $stmt->bindValue($key, $val, PDO::PARAM_STR); }
- }
- if(!$stmt->execute()) { print_r($stmt->errorInfo()); }
- }
- else {
- if(!$stmt->execute()) { print_r($stmt->errorInfo()); }
- }
- return $stmt;
- }
- // Count Number of Rows Function
- // Query needs to have this syntax "SELECT SQL_CALC_FOUND_ROWS columns...."
- function count_rows() {
- $table_rows = $this->conn->query('SELECT FOUND_ROWS()');
- $count = $table_rows->fetchColumn();
- return $count;
- }
- // Create a UUID
- function uuid() {
- $stmt = $this->conn->query("SELECT UUID()");
- $row = $stmt->fetch(PDO::FETCH_NUM);
- return $row[0];
- }
- }
- ?>
Add Comment
Please, Sign In to add comment