1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
| class DB {
private $db;
private $sql='';
public $debug=false;
private $replace=array();
// initialize connection
function __construct (){
try {
$dsn="mysql:dbname=".DB_NAME .";host=".DB_HOST ;
$this-> ;db = new PDO ($dsn, DB_USER , DB_PASS );
} catch (PDOException $e) {
print "Error!: " . $e-> ;getMessage () . "
";
die();
}
}
private function build_where ($where){
if(!empty($where)){
$this-> ;sql .=" WHERE ";
$c=count($this-> ;replace );
foreach($where as $key=> ;$w){
// look for any comparitive symbols within the where array value.
if(substr($w,0,1)=='%'){
// prep the query for PDO->prepare
$this-> ;sql .=$key.'=%:'.$c.'% && ';
$this-> ;replace [':'.$c]=$w;
}
else{
if(substr($w,0,2)=='<=')
$eq='<=';
elseif(substr($w,0,2)=='>=')
$eq='>=';
elseif(substr($w,0,1)=='>')
$eq='>';
elseif(substr($w,0,1)=='<')
$eq='<';
elseif(substr($w,0,1)=='!')
$eq='!=';
else
$eq='=';
// prep the query for PDO->prepare
$this-> ;sql .=$key.$eq.':'.$c.' && ';
$this-> ;replace [':'.$c]=$w;
}
$c++;
}
$this-> ;sql =substr($this-> ;sql ,0,-4);
}
}
// remove slashes from all retrieved variables
private function prep_vars ($vars){
if(is_array($vars)){
foreach($vars as $key=> ;$value)
$ret[$key]=$this-> ;prep_vars ($value);
}
elseif(is_object($vars)){
foreach($vars as $key=> ;$value)
$ret-> ;$key=$this-> ;prep_vars ($value);
}
elseif(is_string($vars)){
$ret=stripslashes($vars);
}
else{
$ret=$vars;
}
return $ret;
}
// general query function
function query ($query,$vals=''){
$sth=$this-> ;db -> ;prepare ($query);
if($vals)
$sth-> ;execute ($vals);
else
$sth-> ;execute ();
$result=$sth-> ;fetchAll (PDO ::FETCH_OBJ);
$e=$sth-> ;errorInfo ();
if($e[0]!='00000'){
if($this-> ;debug ){
if($e[2])
echo '<strong>ERROR:</strong>: '.$e[2];
else
echo '<strong>ERROR:</strong>: General Error';
}
}
if($this-> ;debug )
$this-> ;_get_query ($query,$vals,$e);
return $this-> ;prep_vars ($result);
}
//select and return only one row
function select_one ($table,$vals='*',$where=array(),$extra=''){
$s=$this-> ;select ($table,$vals,$where);
return $s[0];
}
// select function
function select ($table,$vals='*',$where=array(),$extra=''){
// initialize the sql query
$this-> ;replace =array();
$this-> ;sql ="SELECT ";
// add all the values to be selected
if(is_array($vals)){
foreach($vals as $v)
$this-> ;sql .=$v.',';
$this-> ;sql =substr($this-> ;sql ,0,-1);
}
else
$this-> ;sql .=$vals;
$this-> ;sql .=' FROM '.$table;
// build the WHERE portion of the query
$this-> ;build_where ($where);
$this-> ;sql .' '.$extra;
$ret=$this-> ;query ($this-> ;sql ,$this-> ;replace );
return $ret;
}
// insert
function insert ($table,$vals){
// empty the replace array
$this-> ;replace =array();
$this-> ;sql ="INSERT INTO ".$table." SET ";
// build the replace array and the query
$c=count($this-> ;replace );
foreach($vals as $key=> ;$v){
$this-> ;sql .=$key.'=:'.$c.', ';
$this-> ;replace [':'.$c]=$v;
$c++;
}
$this-> ;sql =substr($this-> ;sql ,0,-2);
// run and return the query
$ret=$this-> ;query ($this-> ;sql ,$this-> ;replace );
$id=$this-> ;db -> ;lastInsertId ();
if($id)
return $id;
else
return $ret;
}
// update
function update ($table,$vals,$where=array()){
// empty the replace array
$this-> ;replace =array();
$this-> ;sql ="UPDATE ".$table." SET ";
// build the replace array and the query
$c=count($this-> ;replace );
foreach($vals as $key=> ;$v){
$this-> ;sql .=$key.'=:'.$c.', ';
$this-> ;replace [':'.$c]=$v;
$c++;
}
$this-> ;sql =substr($this-> ;sql ,0,-2);
// build the WHERE portion of the query
$this-> ;build_where ($where);
// run and return the query
return $this-> ;query ($this-> ;sql ,$this-> ;replace );
}
function delete ($table,$where){
// empty the replace array
$this-> ;replace =array();
$this-> ;sql ="DELETE FROM ".$table;
// build the WHERE portion of the query
$this-> ;build_where ($where);
// run and return the query
return $this-> ;query ($this-> ;sql ,$this-> ;replace );
}
// get the number of records matching the requirements
function get_count ($table,$where=false){
// start query
$this-> ;sql ="SELECT COUNT(*) FROM ".$table;
// build the WHERE portion of the query
if($where)
$this-> ;build_where ($where);
// run and return the query
$sth=$this-> ;db -> ;prepare ($query);
if($vals)
$sth-> ;execute ($vals);
// return the row count
return $sth-> ;rowCount ();
}
// gets value of requested column
function get_value ($table,$val,$where=array()){
// run query
$o=$this-> ;select ($table,$val,$where);
// convert first object in associative array to array
$v=get_object_vars($o[0]);
// return requested value
return $v[$val];
}
//debugging function
private function _get_query ($query,$val,$er=0){
echo '
';
foreach($val as $key=> ;$value)
$query=str_replace($key,"'".$value."'",$query);
echo '<strong>QUERY:</strong>
'.$query;
if($er){
echo '
<strong>Raw error:</strong>
<pre>';
print_r($er);
echo '</pre>
';
}
echo '
<hr />
';
}
} |