代码拉取完成,页面将自动刷新
<?php
/**
* mysql db class
* insert($table,$data) 表名,array 插入字段和值
* delete($table,$condition)
* update($table,$data,$condition)表名,array更新字段和值,更新条件
*/
class DB{
private $conn;
private $host = '127.0.0.1';
private $user = 'root';
private $psd = 'myoa888';
public $dbname = 'TD_OA';
function __construct() {
$this->conn = mysqli_connect($this->host, $this->user, $this->psd, $this->dbname, '3336');
}
/**
* 执行一个sql语句,返回mysql资源
* @param type string $sql
* @return type mysql result
*/
function query($sql){
zlog($sql);
return mysqli_query($this->conn, $sql);
}
/**
* 转换传入的数组 为 sql insert
* @param type array $data
* @return type
*/
private function filter_data($data){
$keys = array_keys($data);
$values = array_values($data);
foreach($keys as &$key){
$key="`$key`";
}
foreach($values as &$value){
if(is_string($value)){
$value = "'$value'";
}else if(is_array($value)){
if(is_string($value[1])){
$value[1] = "'{$value[1]}'";
}
}
}
return [$keys,$values];
}
/**
* 返回上一次 插入数据库的id号
* @return type
*/
private function insert_id() {
$id = mysqli_insert_id($this->conn);
return $id;
}
/**
* 传入一个表单名称和数组,将其插入数据库 返回插入后的索引id
* @param type $table
* @param type $data 关联数组
* @return int id
*/
function insert($table,$data){
list($keys,$values) = $this->filter_data($data);
$sql = "INSERT INTO $table(";
$sql .= implode(",",$keys);
$sql .= ") VALUES(";
$sql .= implode(",",$values);
$sql .= ")";
$this->query($sql);
return $this->insert_id();
}
/**
* 返回上一次 query 影响的行数
*/
private function affected_num(){
$num = mysqli_affected_rows($this->conn);
return $num;
}
/**
* 传入一个表名 数组 条件更新一个数据
* @param type $table
* @param type $data
* @param type $condition 更新条件
* @return boolean or int num
*/
function update($table,$data,$condition){
list($keys,$values) = $this->filter_data($data);
$sql = "UPDATE `$table` set ";
$count = count($keys);
for($i=0;$i<$count;$i++){
if($i + 1 == $count){
$sql .= " {$keys[$i]} = {$values[$i]}";
}else{
$sql .= " {$keys[$i]} = {$values[$i]},";
}
}
$sql .= " where $condition";
echo $sql;
$res = $this->query($sql);
if($this->error()){
return false;
}else{
return $this->affected_num();
}
}
/**
* 传入一个表名,条件 删除一条数据
* @param type $table
* @param type $condition
* @return boolean
*/
function delete($table,$condition){
$sql = "DELETE FROM `$table` WHERE $condition";
//echo $sql;
$res = $this->query($sql);
if($this->error()){
return false;
}else{
return $this->affected_num();
}
}
/**
* select 返回 数组
* @param type $table
* @param type $filter
* @param type $condition
* @return boolean
*/
function select($table,$filter="*",$condition="1"){
$sql = "select $filter from $table where $condition";
//echo $sql;
$res = $this->query($sql);
if($this->error()){
return FALSE;
}
$rows = [];
if(mysqli_num_rows($res) == 1){
$row = mysqli_fetch_assoc($res);
return $row;
}elseif (mysqli_num_rows($res) > 1) {
while ($row = mysqli_fetch_assoc($res)){
$rows[] = $row;
}
return $rows;
}else{
return false;
}
}
function error(){
return mysqli_errno($this->conn);
}
public function close(){
mysqli_close($this->conn);
}
}
?>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。