时间:2021-07-01 10:21:17 帮助过:32人阅读
function insert($table,$array){
$keys=join(",",array_keys($array));
$vals="‘".join("‘,‘",array_values($array))."‘";
$sql="insert $table ($keys) values({$vals})";
mysqli_query($this->mysqli, $sql);
return mysqli_insert_id($this->mysqli);
}
function update($table,$array,$where=null){
$str=‘‘;
foreach($array as $key=>$val){
if($str==null){
$sep="";
}else{
$sep=",";
}
$str.=$sep.$key."=‘".$val."‘";
}
$sql="update $table set $str".($where==null?null:" where ".$where);
$result=mysqli_query($this->mysqli,$sql);
//var_dump($result);
//var_dump(mysql_affected_rows());exit;
if($result){
return mysqli_affected_rows($this->mysqli);
}else{
return false;
}
}
function delete($table,$where=null){
$where=$where==null?null:" where ".$where;
$sql="delete from $table $where";
mysqli_query($this->mysqli,$sql);
return mysqli_affected_rows($this->mysqli);
}
function fetchOne($sql,$result_type=MYSQLI_ASSOC){
$result=mysqli_query($this->mysqli,$sql);
$row=mysqli_fetch_array($result,$result_type);
return $row;
}
function fetchAll($sql,$result_type=MYSQLI_ASSOC){
$result=mysqli_query($this->mysqli,$sql);
$rows=[];
while(@$row=mysqli_fetch_array($result,$result_type)){
$rows[]=$row;
}
return $rows;
}
function getResultNum($sql){
$result=mysqli_query($this->mysqli,$sql);
return mysqli_num_rows($result);
}
function getInsertId(){
return mysqli_insert_id($this->mysqli);
}
function close(){
mysqli_close($this->mysqli);
}
}
/**
* 此class一些操作
*/
//数据库操作示例
//$test=new Mysql();
//$res=$test->insert("admins",[‘admins_name‘=>‘ccc‘,‘admins_pass‘=>md5(‘ccc‘)]);
//$res=$test->update("admins",[‘admins_name‘=>‘king‘,‘admins_pass‘=>md5(‘king‘)],"admins_id=2");
//$res = $test->delete("admins", "admins_name=‘ccc‘");
/*$res=$test->fetchAll("select * from admins");
var_dump($res);
echo count($res);*/
//echo $test->getResultNum("select * from admins where admins_name=‘ccc‘");
php数据库操作
标签: