<?php # 组装查询语句 $add_time = time(); $ret = select($db,'user','country="***"','uid DESC','0,5'); # 组装插入语句 $add_time = time(); $sql = "INSERT INTO `director` ( name,phone,country,add_time) VALUES ( '一本道','18844441111','***','{$add_time}');"; $ret = insert($db,$sql); # 组装修改语句 $sql = "UPDATE `director` SET name='长江',phone='15500001111' WHERE tid=3;"; $ret = save($db,$sql); # 组装删除语句 $sql = "DELETE FROM `director` WHERE tid=5;"; $ret = delete($db,$sql); # 连接方法 function connect($db){ # 连接数据库 $conn = @mysqli_connect($db['host'],$db['user'],$db['pass'],$db['name'],'3306'); if(!$conn){ # 连接错误,抛出异常 exit('数据库报错: '.mysqli_connect_error()); } return $conn; } # 统计数量 function count_number($db,$table,$where){ $sql = "SELECT COUNT(*) AS count_number FROM ".$table." WHERE ".$where; $return = mysqli_query($db,$sql); $return = mysqli_fetch_assoc($return); # 关闭数据库 mysqli_close($db); return $return['count_number']; } # 查询单个数据 function find_one($db,$table,$filed,$where){ $sql = "SELECT ".$filed." FROM ".$table.' WHERE '.$where.' LIMIT 1'; $return = mysqli_query($db,$sql); $return = mysqli_fetch_assoc($return); # 关闭数据库 mysqli_close($db); return $return; } # 查询单条数据 function find($db,$table,$where,$order){ $sql = "SELECT * FROM ".$table.' WHERE '.$where; if($order){ $sql .= ' ORDER BY '.$order; } $sql .= 'LIMIT 1'; $return = mysqli_query($db,$sql); $return = mysqli_fetch_assoc($return); # 关闭数据库 mysqli_close($db); return $return; } # 查询方法 function select($db,$table,$where,$order,$limit){ $sql = "SELECT * FROM ".$table.' WHERE '.$where; if($order){ $sql .= 'ORDER BY '.$order; } if($limit){ $sql .= ' LIMIT '.$limit; } $return = mysqli_query($db,$sql); if($return){ while ( $row = mysqli_fetch_assoc($return)) { $rows[] = $row; } mysqli_free_result($return); } # 关闭数据库 mysqli_close($db); return $rows; } # 插入方法 function insert($db,$sql){ # 执行插入 $return = mysqli_query($db,$sql); if($return){ $return = mysqli_insert_id($db); } # 关闭数据库 mysqli_close($db); return $return; } # 修改方法 function save($db,$sql){ $return = mysqli_query($db,$sql); # 关闭数据库 mysqli_close($db); return $return; } # 删除方法 function delete($db,$sql){ $return = mysqli_query($db.$sql); # 关闭数据库 mysqli_close($db); return $return; } ?>