Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( !class_exists( 'DB' ) ) {
- class DB {
- /**
- * @var null|mysqli
- */
- private $mysqli = null;
- /**
- * @var null|int
- */
- public $last_num_rows = null;
- /**
- * @var null|int
- */
- public $last_insert_id = null;
- /**
- * @var mysqli_stmt[]
- */
- private $stmt_cache = array();
- /**
- * DB constructor.
- * @param $user
- * @param $password
- * @param $database
- * @param string $host
- */
- public function __construct($user, $password, $database, $host = 'localhost') {
- $this->user = $user;
- $this->password = $password;
- $this->database = $database;
- $this->host = $host;
- }
- /**
- * @return mysqli
- */
- protected function connect() {
- if( !$this->mysqli ){
- $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database);
- @$this->mysqli -> set_charset("utf8");
- }
- return $this->mysqli;
- }
- /**
- * @param $query
- * @return array|false
- */
- public function query($query, $ClassName = null) {
- $db = $this->connect();
- $result = $db->query($query) or die( mysqli_error( $db ) );
- if( $result->num_rows > 0 ){
- $results = array();
- if( $ClassName ){
- while ( $row = $result->fetch_object($ClassName) ) {
- $results[] = $row;
- }
- }else{
- while ( $row = $result->fetch_assoc() ) {
- $results[] = $row;
- }
- }
- }else{
- return false;
- }
- return $results;
- }
- /**
- * @param $table
- * @param $data
- * @param $format
- * @return bool
- */
- public function insert($table, $data, $format) {
- // Check for $table or $data not set
- if ( empty( $table ) || empty( $data ) ) {
- return false;
- }
- // Connect to the database
- $db = $this->connect();
- // Cast $data and $format to arrays
- $data = (array) $data;
- $format = (array) $format;
- // Build format string
- $format = implode('', $format);
- $format = str_replace('%', '', $format);
- list( $fields, $placeholders, $values ) = $this->prep_query($data,$table);
- // Prepend $format onto $values
- array_unshift($values, $format);
- // Prepary our query for binding TODO: comment out the mysqli_error
- $stmt = $this->prepare($db, "INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");
- // Dynamically bind values
- call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
- // Execute the query
- $stmt->execute() or die( mysqli_error( $db ));
- // Check for successful insertion
- if ( $stmt->affected_rows ) {
- $this->last_insert_id = $stmt->insert_id;
- return true;
- }
- return false;
- }
- /**
- * @param $table
- * @param $data
- * @param $format
- * @param $where
- * @param $where_format
- * @return bool
- */
- public function update($table, $data, $format, $where, $where_format) {
- // Check for $table or $data not set
- if ( empty( $table ) || empty( $data ) ) {
- return false;
- }
- // Connect to the database
- $db = $this->connect();
- // Cast $data and $format to arrays
- $data = (array) $data;
- $format = (array) $format;
- // Build format array
- $format = implode('', $format);
- $format = str_replace('%', '', $format);
- $where_format = implode('', $where_format);
- $where_format = str_replace('%', '', $where_format);
- $format .= $where_format;
- list( $fields, $placeholders, $values ) = $this->prep_query($data, $table, 'update');
- //Format where clause
- $where_clause = '';
- $where_values = '';
- $count = 0;
- foreach ( $where as $field => $value ) {
- if ( $count > 0 ) {
- $where_clause .= ' AND ';
- }
- $where_clause .= $field . '=?';
- $where_values[] = $value;
- $count++;
- }
- // Prepend $format onto $values
- array_unshift($values, $format);
- $values = array_merge($values, $where_values);
- // Prepary our query for binding
- $stmt = $this->prepare($sql, "UPDATE {$table} SET {$placeholders} WHERE {$where_clause}");
- // Dynamically bind values
- call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
- // Execute the query
- $stmt->execute();
- // Check for successful insertion
- if ( $stmt->affected_rows ) {
- return true;
- }
- return false;
- }
- /**
- * @param $query
- * @param $data
- * @param $format
- * @param $ClassName
- * @return array
- */
- public function select($query, $data, $format, $ClassName = null) {
- // Connect to the database
- $db = $this->connect();
- //Prepare our query for binding
- $stmt = $this->prepare($db, $query);
- //Normalize format
- $format = implode('', $format);
- $format = str_replace('%', '', $format);
- // Prepend $format onto $values
- array_unshift($data, $format);
- //Dynamically bind values
- call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
- //Execute the query
- $stmt->execute();
- //Fetch results
- $result = $stmt->get_result();
- $this->last_num_rows = $result->num_rows;
- if( $result->num_rows ){
- $results = array();
- if( $ClassName ){
- while ($row = $result->fetch_object($ClassName)) {
- $results[] = $row;
- }
- }else{
- while ($row = $result->fetch_assoc()) {
- $results[] = $row;
- }
- }
- }else{
- $results = false;
- }
- return $results;
- }
- /**
- * @param $query
- * @param $data
- * @param $format
- * @return bool
- */
- public function delete($query, $data, $format) {
- // Connect to the database
- $db = $this->connect();
- //Prepare our query for binding
- $stmt = $this->prepare($db, $query);
- //Normalize format
- $format = implode('', $format);
- $format = str_replace('%', '', $format);
- // Prepend $format onto $values
- array_unshift($data, $format);
- //Dynamically bind values
- call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
- // Execute the query
- $stmt->execute();
- // Check for successful insertion
- if ( $stmt->affected_rows ) {
- return true;
- }
- return false;
- }
- public function SpecialPrepared($query,$data,$format){
- // Connect to the database
- $db = $this->connect();
- // Prepare our query for binding
- $stmt = $this->prepare($db, $query);
- //Normalize format
- $format = implode('', $format);
- $format = str_replace('%', '', $format);
- // Prepend $format onto $values
- array_unshift($data, $format);
- //print_r( $data );
- // Dynamically bind values
- call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
- // Execute the query
- $stmt->execute() or die( mysqli_error( $db ) );
- // Check for successful query
- if ( $stmt->affected_rows ) {
- return true;
- }
- return false;
- }
- /**
- * @param $data
- * @param string $type
- * @return array
- */
- private function prep_query($data, $table, $type='insert') {
- // Instantiate $fields and $placeholders for looping
- $fields = '';
- $placeholders = '';
- $values = array();
- $table = $table.".";
- // Loop through $data and build $fields, $placeholders, and $values
- foreach ( $data as $field => $value ) {
- $field = $table.$field;
- $fields .= "{$field},";
- $values[] = $value;
- if ( $type == 'update') {
- $placeholders .= $field . '=?,';
- } else {
- $placeholders .= '?,';
- }
- }
- // Normalize $fields and $placeholders for inserting
- $fields = substr($fields, 0, -1);
- $placeholders = substr($placeholders, 0, -1);
- return array( $fields, $placeholders, $values );
- }
- /**
- * @param $array
- * @return array
- */
- private function ref_values($array) {
- $refs = array();
- foreach ($array as $key => $value) {
- $refs[$key] = &$array[$key];
- }
- return $refs;
- }
- private function prepare($db, $query) {
- $key = md5($query);
- if (!isset($this->stmt_cache[$key])) {
- $this->stmt_cache[$key] = $db->prepare($query) or die( mysqli_error( $db ) );
- }
- return $this->stmt_cache[$key];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement