数据库连接、增删改查等方法封装

Original 2019-08-30 11:07:03 432
abstract:
'127.0.0.1', 'user'=>'root', 'pass'=>'root', 'name'=>'film' ]; $db = connect($db); //数据插入 //$addTime = time(); ////$sql = "INSERT INTO `film` (filmname,pid,num,addtime) VALUES ('《摩天营救》','1','888','{$addTime}');"; //$sql = "INSERT INTO `film` VALUES (null,'《战狼2》','1','222222','{$addTime}');"; //$ret = insert($db,$sql); //print_r($ret); //数据修改 //$sql = "UPDATE `film` SET filmname = '《未命名电影》' WHERE id=8;"; //$ret = modi($db,$sql); //print_r($ret); //数据删除 //$sql = "DELETE FROM `film` WHERE id = 13;"; //$ret = del($db,$sql); //print_r($ret); //数据查询 //$sql = "SELECT * FROM `film` WHERE id = 2;"; //$sql = "SELECT * FROM `film` ORDER BY id ASC LIMIT 0,5;"; //$sql = "SELECT * FROM `film` ORDER BY id DESC LIMIT 0,5;"; //$ret = select($db,'film','pid = 1','id DESC','0,5'); //print_r($ret); //查询一条数据 //$sql = "SELECT * FROM `film` LIMIT 1;"; //$return = mysqli_query($db,$sql); //$return = mysqli_fetch_assoc($return); //$return = first($db,'film','pid = 1'); //print_r($return); //查询一个数据 //$return = one($db,'film','id=2'); //print_r($return); //统计数据 $return = count_num($db,'film'); print_r($return); //数据库连接方法 function connect ($db) { $db = @mysqli_connect($db['host'],$db['user'],$db['pass'],$db['name'],'3306'); if (!$db) { exit('数据库报错:' .mysqli_connect_error()); } return $db; } //插入方法 function insert ($db,$sql) { $return = mysqli_query($db,$sql); if ($return) { $return = mysqli_insert_id($db); } mysqli_close($db); return $return; } //修改方法 function modi ($db,$sql) { $return = mysqli_query($db,$sql); mysqli_close($db); return $return; } //删除方法 function del ($db,$sql) { $return = mysqli_query($db,$sql); mysqli_close($db); return $return; } //查询方法 function selects ($db,$sql) { $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 select ($db,$table,$where,$order,$limit) { // $sql = "SELECT * FROM" .' '.$table .' WHERE ' .$where .' ORDER BY '.$order; $sql = "SELECT * FROM $table WHERE $where 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 first ($db,$table,$where) { $sql = "SELECT * FROM $table WHERE $where LIMIT 1"; $return = mysqli_query($db,$sql); if ($return) { $return = mysqli_fetch_assoc($return); } mysqli_close($db); return $return; } //查询单个数据方法 function one ($db,$table,$where) { $sql = "SELECT filmname FROM $table WHERE $where LIMIT 1"; $return = mysqli_query($db,$sql); if ($return) { $return = mysqli_fetch_assoc($return); } mysqli_close($db); return $return; } //统计数量方法 function count_num ($db,$table) { $sql = "SELECT COUNT(*) AS count_num FROM $table"; $return = mysqli_query($db,$sql); if ($return) { $return = mysqli_fetch_assoc($return); } mysqli_close($db); return $return['count_num']; }


Correcting teacher:天蓬老师Correction time:2019-08-30 16:18:24
Teacher's summary:sql语句中, 关键字建议全部加上反引号

Release Notes

Popular Entries