PHP Classes

File: class.db.mysqli.php

Recommend this page to a friend!
  Classes of JImmy Bo   Bobo PHP DB Class   class.db.mysqli.php   Download  
File: class.db.mysqli.php
Role: Class source
Content type: text/plain
Description: MYSQLi Version :: Bobo PHP DB Class
Class: Bobo PHP DB Class
Simple MySQL database class
Author: By
Last change: MYSQLi Version
Date: 7 years ago
Size: 12,502 bytes
 

Contents

Class file image Download
<?php /* Version 1.2 - mysqli version - include this file instead of class.db.php if you want to use mysqli to connect */ /* * Please leave this comment in at the top of the script if you use this script * in it's entirety. That is my only request. Other then that have fun. * http://www.phpclasses.org/package/5283-PHP-Simple-MySQL-database-access-wrapper.html * * * example usage: * * 1) create a file called inc.config.php in it throw this: * // include the class: * include_once('class.db.mysqli.php'); * // set the variables: * $db_host = "localhost"; * $db_user = "myusername"; * $db_pass = "mypassword"; * $db_name = "mydatabasename"; * // make a db object: * $db = new mydb($db_host, $db_user, $db_pass, $db_name); * * 2) now in your file that you want to access the db from just include_once('inc.config.php'); * don't worry it won't connect till it's first query and it does so automagically. * * 3) some example usage: * // an insert * $SQL = "INSERT INTO addressbook (name) VALUES ('bob')"; * $insertid = $db->q($SQL); // returns false if no insertid or the id * echo $insertid; * * // a single row returned * $SQL = "SELECT * FROM addressbook WHERE name LIKE 'bob' LIMIT 1"; * $row = $db->r($SQL); * echo $row['name']; * * // multiple rows returned * $SQL = "SELECT * FROM addressbook"; * $addresses = $db->get($SQL); * foreach($addresses AS $address) * echo $address['name']; * * * * Also if you're looking for help from me directly you can usually find me * slumming it on #phphelp on the undernet irc network. * in that case I'd love to still hear from you at my email listed above. * * connection types: (can set on class init [mydb($host,$user,$pass, $db, $contype)]) or function set_contype($contype=0). If you call set_contype if there is a connection present it will reset it. -=-=-=-=-=-=-=-=-=-=- 1: Persistent Connection 2: Persistent Connection w/compression 3: Regular Connection w/compression 0: Regular Connection */ // MYSQLi VERSION!!!! class mydb { var $connect; // stores our db connection var $db_user, $db_pass, $db_host, $db_name; var $contype; function mydb($db_host, $db_user, $db_pass, $db_name) { // connect: $this->db_user = $db_user; $this->db_pass = $db_pass; $this->db_host = $db_host; $this->db_name = $db_name; $this->contype = 0; $this->connect(); } // CONSTRUCTOR /* BEGIN DATABASE FUNCTIONS */ function connect() { if($this->is_connected()) // check for open connection return true; // connection exists :: bail // connect to our database /* $this->connect = mysqli_connect( $this->db_host , $this->db_user , $this->db_pass, $this->db_name ); */ switch($this->contype) { case 1: ###################################### Persistent Connection: $this->connect = mysqli_pconnect($this->host, $this->user, $this->pass) or die(mysql_error()."<div>error connecting [persistent]</div>\r\n"); break; /* case 2:####################### Persistent Connection with Compression: $this->connect = mysqli_real_connect ( $this->db_host, $this->db_user, $this->db_pass, $this->db_name, MYSQL_CLIENT_COMPRESS ) or die(mysql_error()."<div>error connecting [persistent-compress]</div>\r\n"); break; case 3: ######################### Regular Connection with Compression: $this->connect = mysqli_real_connect ( $this->db_host, $this->db_user, $this->db_pass, $this->db_name, MYSQL_CLIENT_COMPRESS ) or die(mysql_error()."<div>error connecting [compress]</div>\r\n"); break; */ default: #################################### Regular Connection: $this->connect = mysqli_connect( $this->db_host , $this->db_user , $this->db_pass, $this->db_name ) or die(mysql_error()."<div>error connecting [default]</div>\r\n"); break; } return true; } // $this->connect(); function close() { if($this->is_connected()) { @mysql_close($this->connect); $this->connect = false; return 1; } } // $this->close(); function is_connected() { if( isset($this->connect) && !empty($this->connect) ) return true; } // $this->is_connected(); function set_contype($contype=0) { $oldtype = $this->contype; if($contype > 0 && $contype < 4) $this->contype = $contype; else $this->contype = 0; // check if there is a difference between the old connection type // and the new connection type. Then check to see if there is a connection // open and if so then close and reopen it. if($oldtype != $this->contype) { if($this->is_connected()) { $db->close(); $db->connect(); } } } // $this->set_contype($contype=0); function q($sql) // inserts - updates { $q = mysqli_query($this->connect, $sql); return $q; // returns an id if an insert } // $this->q($sql); function get($sql) // selects { $q = mysqli_query($this->connect, $sql); $uData = false; do //show admin options { if(is_array($uData)) $tarray[] = $uData; } while($uData = mysqli_fetch_assoc($q)); if(!empty($tarray) && is_array($tarray)) return $tarray; } // $this->get($sql); function r($sql) // selects with only one result { $q = mysqli_query($this->connect, $sql); $uData = false; do //show admin options { if(is_array($uData)) return $uData; } while($uData = mysqli_fetch_assoc($q)); } // $this->r($sql); function flush_buffers() { ob_end_flush(); ob_flush(); flush(); ob_start(); } // $this->flush_buffers(); // yes I'm sure this one can be super duper optimized. function safe_text($text) // convert special characters into html escape codes { $text = mysql_real_escape_string($text); return $text; } // $this->safe_text($text); // will take the javascript out of your text function remove_js($text) { return preg_replace("/<script[^>]*>.*?< *script[^>]*>/i", "", $text); } // $this->remove_js($text); // will take the html out of your text function remove_html($text) { return strip_tags($text); } // $this->remove_html($text); // batch functions [ return SQL ] ## EXAMPLE UPDATE ## /* global $db; $table = 'tblorderpcs'; $extra = "WHERE jID = $subjobid LIMIT 1"; $arr = array ( "pcs_name" => $sname, "pcs_qty" => $sqty, "pcs_pn" => $spn, "pcs_estimate" => $sest, "pcs_memo" => $smemo, "ship_id" => $ship_id, "pcs_made" => $smade, "pcs_wasted"=> $swasted, "pcs_ppu" => $sppu ); return $db->update_from_array($table, $arr, $extra); */ function update_from_array_sql($table, $arr, $extra) { if(!is_array($arr) || empty($table)) return false; $first = true; $sql = "UPDATE $table SET "; foreach($arr as $key => $value) { if(!$first) $sql .= ","; $sql .= $key."="."'$value' "; $first = false; } $sql .= $extra; return $sql; } // $this->update_from_array_sql($table, $arr, $extra); function update_from_array($table, $arr, $extra) { $sql = $this->update_from_array_sql($table, $arr, $extra); return $this->q($sql); } // $this->update_from_array($table, $arr, $extra); function insert_from_array_of_arrays_sql($table, $arrarr) // ex: $arr[0] = array('id' => 5, 'name' => 'bob'); { if(!is_array($arrarr) || empty($table)) return false; $key_str = ''; $val_str = ''; $sql_start = "\r\nINSERT INTO $table ( "; $sql_mid = " ) VALUES "; $sql_end = " ; \r\n"; $arrarrfirst = true; $arrarrvalstr = ''; foreach($arrarr as $arrarrkey => $arr) { $val_str = ''; $first = true; // for sub array navigation if(!$arrarrfirst) $val_str .= ','; $val_str = '('; foreach($arr as $key => $value) { if(!$first) { if($arrarrfirst) // just want key string built off first entry // $key_str .= ","; $val_str .= ","; } if($arrarrfirst) // just want key string built off first entry // $key_str .= "$key"; $val_str .= "'$value'"; $first = false; } $val_str .= ')'; if(!$arrarrfirst) $arrarrvalstr .= ", \r\n"; $arrarrvalstr .= $val_str; $arrarrfirst = false; } $sql = $sql_start.$key_str.$sql_mid.$arrarrvalstr.$sql_end; return $sql; } // $this->insert_from_array_of_arrays_sql($table, $arrarr); function insert_from_array_of_arrays($table, $arrarr) // ex: $arr[0] = array('id' => 5, 'name' => 'bob'); { $sql = $this->insert_from_array_of_arrays_sql($table, $arrarr); $id = $this->q($sql); } // $this->insert_from_array_of_arrays($table, $arrarr); // ex: $arr[0] = array('id' => 5, 'name' => 'bob'); ## EXAMPLE INSERT ## /* global $db; $time = time(); $table = 'tblorderpcs'; $arr = array ( "order_id" => $jobid, "pcs_name" => $sname, "pcs_qty" => $sqty, "pcs_pn" => $spn, "pcs_estimate" => $sest, "pcs_memo" => $smemo, "ship_id" => $ship_id, "pcs_tstamp" => $time ); return $db->insert_from_array($table, $arr); */ function insert_from_array_sql($table, $arr) { if(!is_array($arr) || empty($table)) return false; $key_str = ''; $val_str = ''; $sql_start = "\r\nINSERT INTO $table ( "; $sql_mid = " ) VALUES ( "; $sql_end = " ); \r\n"; $first = true; foreach($arr as $key => $value) { if(!$first) { $key_str .= ","; $val_str .= ","; } $key_str .= "$key"; $val_str .= "'$value'"; $first = false; } $sql = $sql_start.$key_str.$sql_mid.$val_str.$sql_end; return $sql; } // $this->insert_from_array_sql($table, $arr); function insert_from_array($table, $arr) { $id = $this->q( $this->insert_from_array_sql($table, $arr) ); return $id; } // $this->insert_from_array($table, $arr); function table_exists($table) { if(!$this->is_connected()) $this->connect(); $sql = 'SHOW TABLES FROM `'.$this->db_name.'` LIKE \''.$table.'\' '; // echo "<div>".$sql."</div>"; $row = $this->r($sql); if(is_array($row)) return true; } // BEGIN -> compatibility functions function query($sql) { return $this->q($sql); } function query_return($sql) { return $this->get($sql); } function row($sql) { return $this->r($sql); } // END -> compatibility functions } // end class mydb (mysqli version) ?>