Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Config */
- const DB_HOST = 'localhost';
- const DB_USER = 'root';
- const DB_PASS = '';
- const DB_NAME = 'testDb';
- const DB_CHARSET = 'utf8';
- const DEBUG = 1;
- /* New DB instance */
- $db = new DB();
- /* Test variables */
- $id = 1;
- $something = "456";
- /* Prepared statement */
- $query = 'SELECT * FROM `test1` WHERE `id` = ? AND `something` = ?';
- $sql = $db->query(
- $query,
- array($id, $something)
- );
- /* Normal query */
- $query = 'SELECT * FROM `test2` WHERE `id` = 1';
- $sql = $db->query($query);
- /* Fetch data */
- $data = $db->fetch_all();
- /* Display result */
- print_r($data);
- /* Close db */
- $db->close();
- /**
- * MySQLi db class by Andrew Larsen
- */
- class DB
- {
- private $mysqli;
- private $stmt;
- private $lastPrepared;
- public function __construct($host = DB_HOST, $user = DB_USER, $pass = DB_PASS, $dbname = DB_NAME, $charset = DB_CHARSET){
- $this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
- if (!$this->mysqli) {
- if (DEBUG)
- echo 'Code: ' . mysqli_connect_errno() . ' - Message: ' . mysqli_connect_error();
- return false;
- }
- $this->mysqli->set_charset(DB_CHARSET);
- }
- private function bind($parameters) {
- $param_type = '';
- $param_values = array();
- foreach ($parameters as $key => $value) {
- $param_values[] = $value;
- if (is_string($value))
- $param_type .= 's'; // string
- else if (is_int($value))
- $param_type .= 'i'; // int
- else if (is_float($value))
- $param_type .= 'd'; // double
- else
- $param_type .= 'b'; // blob and unknown
- }
- $params = array();
- /* with call_user_func_array, array params must be passed by reference */
- $params[] = &$param_type;
- for ($i = 0; $i < count($param_values); $i++) {
- /* with call_user_func_array, array params must be passed by reference */
- $params[] = &$param_values[$i];
- }
- call_user_func_array(array($this->stmt, 'bind_param'), $params);
- }
- private function prepare($query) {
- if (!$this->stmt = $this->mysqli->prepare($query)) {
- if (DEBUG)
- echo "Prepare failed: (" . $this->mysqli->errno . ") " . $this->mysqli->error;
- return false;
- }
- return true;
- }
- private function execute() {
- if (!$this->stmt->execute()) {
- if (DEBUG)
- echo "Execute failed: (" . $this->stmt->errno . ") " . $this->stmt->error;
- return false;
- }
- return true;
- }
- public function query($query, $parameters = array()) {
- if (is_array($parameters) && count($parameters) > 0) {
- $this->lastPrepared = true;
- if ($this->prepare($query) === false)
- return false;
- $this->bind($parameters);
- if ($this->execute() === false)
- return false;
- } else {
- $this->lastPrepared = false;
- if (!$this->stmt = $this->mysqli->query($query)) {
- if (DEBUG)
- echo "Query failed: (" . $this->mysqli->errno . ") " . $this->mysqli->error;
- return false;
- }
- }
- return $this->stmt;
- }
- public function fetch_all($autoClean = true) {
- if ($this->lastPrepared) {
- $result = $this->stmt->get_result();
- $data = $result->fetch_all(MYSQLI_ASSOC);
- } else {
- $data = $this->stmt->fetch_all(MYSQLI_ASSOC);
- }
- if ($autoClean) {
- $this->stmt->free_result();
- if ($this->lastPrepared) $this->stmt->close();
- }
- if (count($data) == 1)
- return $data[0];
- return $data;
- }
- public function begin($option = 'MYSQLI_TRANS_START_READ_ONLY') {
- $options = array(
- 'MYSQLI_TRANS_START_READ_ONLY',
- 'MYSQLI_TRANS_START_READ_WRITE',
- 'MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT'
- );
- if (!in_array($option, $options))
- return false;
- $this->mysqli->begin_transaction($option);
- }
- public function commit() {
- $this->mysqli->commit();
- }
- public function autocommit($bool) {
- $this->mysqli->autocommit($bool);
- }
- public function close() {
- $this->mysqli->close();
- }
- public function stmtClose()
- {
- $this->stmt->close();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement