Jeff PHP framework
0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
|
00001 <?php 00021 class mysql implements DbManager { 00022 00026 private $_db_host; 00027 00031 private $_db_user; 00032 00036 private $_db_pass; 00037 00041 private $_db_dbname; 00042 00046 private $_db_charset; 00047 00051 private $_connection; 00052 00059 function __construct($params) { 00060 00061 $this->_db_host = $params["host"]; 00062 $this->_db_user = $params["user"]; 00063 $this->_db_pass = $params["password"]; 00064 $this->_db_dbname = $params["db_name"]; 00065 $this->_db_charset = $params["charset"]; 00066 00067 if($params["connect"]===true) { 00068 $this->openConnection(); 00069 } 00070 00071 } 00072 00079 public function escapeString($string) { 00080 00081 return mysql_real_escape_string($string); 00082 00083 } 00084 00090 public function openConnection() { 00091 00092 if($this->_connection = mysql_connect($this->_db_host, $this->_db_user, $this->_db_pass)) { 00093 00094 @mysql_select_db($this->_db_dbname, $this->_connection) 00095 OR exit("Db selection error"); 00096 00097 if($this->_db_charset=='utf8') $this->setUtf8(); 00098 00099 } 00100 else 00101 exit("Db connection error"); 00102 } 00103 00109 private function setUtf8() { 00110 00111 $db_charset = $this->executeQuery( "SHOW VARIABLES LIKE 'character_set_database'" ); 00112 $charset_row = mysql_fetch_assoc( $db_charset ); 00113 $this->executeQuery( "SET NAMES '" . $charset_row['Value'] . "'" ); 00114 unset( $db_charset, $charset_row ); 00115 00116 } 00117 00124 public function executeQuery($query) { 00125 00126 $res = mysql_query($query); 00127 00128 return $res; 00129 00130 } 00131 00138 public function queryResult($query) { 00139 00140 $results = array(); 00141 00142 $res = $this->executeQuery($query); 00143 00144 if($res) { 00145 while($row = mysql_fetch_assoc($res)) { 00146 $results[] = $row; 00147 } 00148 mysql_free_result($res); 00149 } 00150 00151 return $results; 00152 00153 } 00154 00160 public function getError() { 00161 00162 $error = mysql_error(); 00163 00164 // duplicate 00165 if(preg_match("#^Duplicate entry '(.*?)' .*? key (\d+)$#", $error, $matches)) { 00166 return array("error"=>1001, "key"=>$matches[2], "value"=>$matches[1]); 00167 } 00168 00169 } 00170 00181 public function autoSelect($fields, $tables, $where, $order=null, $limit=null) { 00182 00183 $qfields = is_array($fields) ? implode(",", $fields):$fields; 00184 $qtables = is_array($tables) ? implode(",", $tables):$tables; 00185 $qwhere = $where ? "WHERE ".$where : ""; 00186 $qorder = $order ? "ORDER BY $order":""; 00187 $qlimit = count($limit) ? "LIMIT ".$limit[0].",".$limit[1]:""; 00188 00189 $query = "SELECT $qfields FROM $qtables $qwhere $qorder $qlimit"; 00190 00191 return $this->queryResult($query); 00192 00193 } 00194 00203 public function getNumRecords($table, $where=null, $field='id') { 00204 00205 $tot = 0; 00206 00207 $qwhere = $where ? "WHERE ".$where : ""; 00208 $query = "SELECT COUNT($field) AS tot FROM $table $qwhere"; 00209 $res = $this->executeQuery($query); 00210 if($res) { 00211 while($row = mysql_fetch_assoc($res)) { 00212 $tot = $row['tot']; 00213 } 00214 mysql_free_result($res); 00215 } 00216 00217 return (int) $tot; 00218 00219 } 00220 00227 public function getFieldsName($table) { 00228 00229 $fields = array(); 00230 $query = "SHOW COLUMNS FROM ".$table; 00231 00232 $res = $this->executeQuery($query); 00233 while($row = mysql_fetch_assoc($res)) { 00234 $results[] = $row; 00235 } 00236 mysql_free_result($res); 00237 00238 foreach($results as $r) {$fields[] = $r['Field'];} 00239 00240 return $fields; 00241 00242 } 00243 00263 public function getTableStructure($table) { 00264 00265 $structure = array("primary_key"=>null, "keys"=>array()); 00266 $fields = array(); 00267 00268 $query = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$this->_db_dbname."' AND TABLE_NAME = '$table'"; 00269 $res = $this->executeQuery($query); 00270 00271 while($row = mysql_fetch_array($res)) { 00272 preg_match("#(\w+)\((\d+),?(\d+)?\)#", $row['COLUMN_TYPE'], $matches); 00273 $fields[$row['COLUMN_NAME']] = array( 00274 "order"=>$row['ORDINAL_POSITION'], 00275 "default"=>$row['COLUMN_DEFAULT'], 00276 "null"=>$row['IS_NULLABLE'], 00277 "type"=>$row['DATA_TYPE'], 00278 "max_length"=>$row['CHARACTER_MAXIMUM_LENGTH'], 00279 "n_int"=>isset($matches[2]) ? $matches[2] : 0, 00280 "n_precision"=>isset($matches[3]) ? $matches[3] : 0, 00281 "key"=>$row['COLUMN_KEY'], 00282 "extra"=>$row['EXTRA'] 00283 ); 00284 if($row['COLUMN_KEY']=='PRI') $structure['primary_key'] = $row['COLUMN_NAME']; 00285 if($row['COLUMN_KEY']!='') $structure['keys'][] = $row['COLUMN_NAME']; 00286 } 00287 $structure['fields'] = $fields; 00288 00289 return $structure; 00290 } 00291 00298 public function getTables($like=null) { 00299 00300 $tables = array(); 00301 $query = "SHOW TABLES".($like ? " LIKE '$like'":''); 00302 00303 $res = $this->executeQuery($query); 00304 while($row = mysql_fetch_array($res)) { 00305 $results[] = $row; 00306 } 00307 mysql_free_result($res); 00308 00309 foreach($results as $r) {$tables[] = $r[0];} 00310 00311 return $tables; 00312 00313 } 00314 00322 public function insert($table, $data) { 00323 00324 $fields = array(); 00325 $values = array(); 00326 00327 foreach($data as $f=>$v) { 00328 $fields[] = $f; 00329 $values[] = "'$v'"; 00330 } 00331 00332 $query = "INSERT INTO ".$table." (`".implode("`,`", $fields)."`) VALUES (".implode(",", $values).")"; 00333 $result = $this->executeQuery($query); 00334 00335 return $result ? $this->lastInsertedId() : false; 00336 } 00337 00346 public function update($table, $data, $where) { 00347 00348 if(!$data) return true; 00349 00350 $sets = array(); 00351 foreach($data as $f=>$v) $sets[] = is_null($v) ? "`$f`=NULL" : "`$f`='$v'"; 00352 $query = "UPDATE ".$table." SET ".implode(",", $sets)." ".($where ? "WHERE $where":""); 00353 00354 $result = $this->executeQuery($query); 00355 00356 return $result; 00357 } 00358 00364 private function lastInsertedId() { 00365 00366 if(mysql_affected_rows()) return mysql_insert_id(); 00367 else return false; 00368 00369 } 00370 00378 public function delete($table, $where) { 00379 00380 $query = "DELETE FROM $table ".($where ? "WHERE $where":""); 00381 00382 $result = $this->executeQuery($query); 00383 00384 return $result; 00385 } 00386 } 00387 00388 00389 ?>