Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Simple PDO wrapper to ease database interactions, always returning multi-dimensional array for consistency
- class PDOWrapper
- {
- public $dbc;
- // Establish database connection
- public function __construct($db_host, $db_name, $db_user, $db_pass)
- {
- try {
- $this->dbc = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
- return $this->dbc;
- }
- catch (Exception $error) {
- return $error;
- }
- } // END constructor method
- // Force multi-dimensional array
- public function return_array($data)
- {
- $return_array = [];
- return $return_array['data'] = $data;
- } // END return_array method
- // TODO: Build try/catch method?
- // Get table columns
- public function getColumns($table)
- {
- try {
- $stmt = $this->dbc->prepare("SELECT * FROM " . $table);
- $stmt->execute();
- $array = $stmt->fetch(PDO::FETCH_ASSOC);
- }
- catch (Exception $error) {
- return $error;
- }
- // Parse through returned results and build array of table columns
- $columns_array = [];
- foreach ($array as $column => $field) {
- $columns_array[] = $column;
- }
- // Return multi-dimensional array
- $this->return_array($columns_array);
- } // END getColumns method
- // Show tables in database
- public function getTables()
- {
- $stmt = $this->dbc->prepare("SHOW TABLES;");
- $stmt->execute();
- $results = $stmt->fetch();
- // Return multi-dimensional array
- $this->return_array($results);
- } // END getTables method
- // Query database dynamically
- public function select($table, $where_assoc_array = [], $limit = 'display_all')
- {
- // Handle default 'display_all' value given for human reliability
- if ($limit == 'display_all') {
- $limit = '';
- }
- else {
- $limit = ' LIMIT ' . $limit;
- }
- // Build 'where' clause
- if (!empty($where_assoc_array)) {
- $where_clause = 'WHERE ';
- foreach ($where_assoc_array as $column => $field) {
- $where_clause .= $column . " = " . $field . " AND ";
- }
- // Trim off extra 'AND' off end of string
- rtrim($where_clause, ' AND ');
- }
- else {
- $where_clause = '';
- }
- try {
- // Build statement
- $stmt = $this->dbc->prepare("SELECT * FROM" . $table . $where_clause . $limit . ";");
- $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
- // Return multi-dimensional array
- return $this->return_array($results);
- }
- catch (Exception $error) {
- return $this->return_array($error);
- }
- } // END select method
- // Insert into database dynamically
- public function insert($table, $assoc_array)
- {
- $columns_array = [];
- $values_array = [];
- // Parse array given
- foreach ($assoc_array as $key => $value) {
- $columns_array[] = $key;
- $values_array[] = $value;
- }
- // Build insert statement
- $stmt = $this->dbc->prepare("INSERT INTO" . $table . " (" . $columns_array . ") VALUES (" . $values_array . ");");
- $stmt->execute();
- $results = $stmt->fetch();
- // Return multi-dimensional array
- $this->return_array($results);
- } // END insert method
- // Update values dynamically
- public function update($table, $assoc_array, $where_assoc_array = [])
- {
- $stmt_middle = '';
- // Parse array given
- foreach ($assoc_array as $key => $value) {
- $stmt_middle .= $key . " = '" . $value . "', ";
- }
- // Trim off extra comma
- rtrim($stmt_middle, ', ');
- // Build statement
- $stmt_begin = "UPDATE" . $table . "SET ";
- $stmt_end = "WHERE ";
- // Parse array to build 'WHERE' clause
- foreach ($where_assoc_array as $key => $value) {
- $stmt_end .= $key . " = " . $value . " AND ";
- }
- // Trim off extra 'AND' off end of string
- rtrim($stmt_end, ' AND ');
- try {
- // Build update statement
- $stmt = $this->dbc->prepare($stmt_begin . $stmt_middle . $stmt_end);
- $stmt->execute();
- $results = $stmt->fetch();
- // Return multi-dimensional array
- $this->return_array($results);
- }
- catch (Exception $error) {
- $this->return_array($error);
- }
- } // END update method
- // Delete rows from table
- public function delete($table, $assoc_array)
- {
- // TODO:
- } // END delete method
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement