mr.zherart
Новичок
Здравствуйте, написал собственный велосипед. Стараюсь не писать говнокод. Оцените насколько тут получилось. И конечно же, если это плохой код, укажите на ошибки! Спасибо!
PHP:
class DB extends mysqli {
private $typeServer = 0; // 0 - Locale, 1 - Real
static $instances = 0; // 0 - connection not set, 1 - connection already set
private $connect;
/* Params for real Server (rServer)*/
private $rServer = "";
private $rUser = "";
private $rPassword = "";
private $rDatabase = "";
/* Params for Local Server (lServer) */
private $lServer = "localhost";
private $lUser = "root";
private $lPassword = "";
private $lDatabase = "develophouse";
function __construct(){
/* Connect to DB*/
if($this->typeServer == 0 && DB::$instances == 0){
$this->connect = new MySQLi($this->lServer, $this->lUser, $this->lPassword, $this->lDatabase);
}elseif($this->typeServer == 1 && DB::$instances == 0){
$this->connect = new MySQLi($this->rServer, $this->rUser, $this->rPassword, $this->rDatabase);
}else{
echo "Connection already set, close connection";
}
$this->connect->set_charset("utf8");
if($this->connect->errno){
exit('Connection to server unpossible. Error code: '.$this->connect->error.'\n');
}else{DB::$instances = 1;}
}
/*
*
* @query - query string to db
* @arrat_type - type of return:
* simple,single - just one string
* assoc, row, object - one array
* mysqli_* all query data
*/
function do_query($query, $array_type=''){
$result = $this->connect->query($query);
if($result){
switch ($array_type){
case 'simple':
case 'single':
return implode($result->fetch_row());
case 'assoc':
case 'row':
case 'object':
return $result->{'fetch_'.$array_type}();
case MYSQLI_ASSOC:
case MYSQLI_NUM:
case MYSQLI_BOTH:
return $result->fetch_all($array_type);
default:
return $result;
}
}else{
return $this->connect->error;
}
}
/*
* @arr - number, string or array to protect from SQL injection
*/
function protect_query($arr){
switch ($arr){
case (is_array($arr)):
$result = $this->inArray($arr);
break;
case (is_numeric($arr)):
return sprintf('%d', $arr);
case (is_string($arr)):
return mysql_real_escape_string($arr);
}
return $result;
}
private function inArray($arg){
foreach ($arg as $key => $value) {
switch ($value){
case (is_array($value)):
$protected[$key] = $this->inArray($value);
break;
case (is_numeric($value)):
$protected[$key] = sprintf('%d', $value);
break;
case (is_string($value)):
$protected[$key] = mysql_real_escape_string($value);
break;
}
}
return $protected;
}
function closeDB(){
$this->connect->close();
DB::$instances = 0;
}
}
Последнее редактирование: