예 클래스 mysqli 래퍼 :
<?php
/**
* New Version 24/10/2014
*
* mb_internal_encoding('UTF-8');
* mb_regex_encoding('UTF-8');
*/
class miDB{
private $result;
private $db;
// filter_var_array($set,FILTER_SANITIZE_STRING)
public function __construct($host,$user,$pass,$bbdd){
$this->db = new mysqli($host,$user,$pass,$bbdd);
$this->db->set_charset("utf8");
if ($this->db->connect_errno) {
throw new Exception ("Falló la conexión: ".$this->db->connect_error);
}
}
/**
*
* @param string $sql
* @param array $datos
* @param object $stmt
*
* @return
*/
private function preparaQuery($datos){
$tipos = '';
$conta = 1;
foreach($datos as $v){
if (is_int($v)) {
$tipos .= 'i';
}elseif(is_bool($v)){
$tipos .= 'i';
}elseif(is_float($v) || is_double($v)){
$tipos .= 'd';
}elseif('blob' == gettype($v)){
$tipos .= 'b';
}else{
$tipos .= 's';
}
}
$paramsDatos = array_merge(array($tipos), $datos);
$params = array();
foreach($paramsDatos as $i=>$v){
$params[$i] = &$paramsDatos[$i];
}
return $params;
}
/**
*
* @param string $query
* @param array $datos
*
* @return
*/
public function insert($query,$datos=false){
if(is_array($datos)){
//consulta preparada
if($stmt = $this->db->prepare($query)){
call_user_func_array(array($stmt, 'bind_param'), $this->preparaQuery($datos));
if($stmt->execute()){
$result = $stmt->affected_rows;
}else{
throw new Exception ("Falló la consulta: ".$this->db->error);
}
if($result >= 1){
$id = $stmt->insert_id;
$stmt->close();
return $id;
}else{
$stmt->close();
return false;
}
}else{
throw new Exception ("Falló : ".$this->db->error.' - '.$stmt->error);
}
}else{
if($this->db->query($query)){
return $this->db->insert_id;
}else{
return false;
}
}
}
/**
*
* @param string $query
* @param array $datos
*
* @return
*/
public function update($query,$datos=false){
if(is_array($datos)){
//consulta preparada
if($stmt = $this->db->prepare($query)){
call_user_func_array(array($stmt, 'bind_param'), $this->preparaQuery($datos));
if($stmt->execute()){
//$result = $stmt->get_result();
$result = $stmt->affected_rows;
$stmt->close();
}else{
throw new Exception ("Falló la consulta: ".$this->db->error.' - '.$stmt->error);
}
if($result >= 1){
return $result;
}else{
return false;
}
}else{
throw new Exception ("Falló : ".$this->db->error);
}
}else{
$this->db->query($query);
return $this->db->affected_rows;
}
}
/**
*
* @param string $query
* @param array $datos
*
* @return
*/
public function delete($query,$datos=false){
if(is_array($datos)){
//consulta preparada
if($stmt = $this->db->prepare($query)){
call_user_func_array(array($stmt, 'bind_param'), $this->preparaQuery($datos));
if($stmt->execute()){
//$result = $stmt->get_result();
$result = $stmt->affected_rows;
$stmt->close();
}else{
throw new Exception ("Falló la consulta: ".$this->db->error.' - '.$stmt->error);
}
if($result >= 1){
return $result;
}else{
return false;
}
}else{
throw new Exception ("Falló : ".$this->db->error);
}
}else{
$this->db->query($query);
return $this->db->affected_rows;
}
}
/**
*
* @param string $query
* @param array $datos
*
* @return
*/
public function select($query,$datos=false){
if(is_array($datos)){
//consulta preparada
if($stmt = $this->db->prepare($query)){
call_user_func_array(array($stmt, 'bind_param'), $this->preparaQuery($datos));
if($stmt->execute()){
$result = $stmt->get_result();
}else{
throw new Exception ("Falló la consulta: ".$this->db->error);
}
$miResult = array();
while ($fila = $result->fetch_assoc()) {
$miResult[] = $fila;
}
$result->free();
$stmt->close();
return $miResult;
}else{
throw new Exception ("Falló : ".$this->db->error);
}
}else{
// consulta sin preparar
if ($result = $this->db->query($query)) {
$miResult = array();
$result->data_seek(0);
while ($fila = $result->fetch_assoc()) {
$miResult[] = $fila;
}
$result->free();
return $miResult;
}else{
throw new Exception ("Falló : ".$this->db->error);
return false;
}
}
}
/**
* Get number of affected rows in previous MySQL operation
* @return integer
*/
public function AffectedRows()
{
return $this->db->affected_rows;
}
/**
* Get number of rows in result
* @return integer
*/
public static function Count()
{
return $this->db->count;
}
/**
* Get the ID generated in the last query
* @return integer
*/
public static function LastId()
{
return $this->db->insert_id;
}
}
?>
사용 :
$db = new miDB('localhost','root','');
$sql = "INSERT INTO myTable (col1, col2, col3) VALUES (?, ?, ?)";
$data = array('foo',350, false);
$db->insert($sql,$data);
난 당신이 [코드 검토 (HTTP를 찾고 생각 : // codereview.stackexchange.com/). –
아마도 전역 변수를 사용하지 말고 아마도 $ mysqli를 인수로 전달할 것입니다. 또는 더 많은 클래스 기반 접근 방식을 사용하십시오. –
Ok Dave, 감사합니다! CodeReview – user3800667