????://mysqli連接 function connMysqli(){ //連接數(shù)據(jù)庫 $db = @mysqli_connect('127.0.0.1','root','123456','test','3
//mysqli連接 function connMysqli(){ //連接數(shù)據(jù)庫 $db = @mysqli_connect('127.0.0.1','root','123456','test','3306'); if(!$db){ //連接出錯,拋出錯誤 exit('數(shù)據(jù)庫出錯:'.mysqli_connect_error()); } //插入數(shù)據(jù) $time = time(); $sql = "insert into `user`(`name`,`sex`,`age`,`email`,`password`,`status`,`create_time`) values('包惜弱',1,18,'hzh@php.com',sha1(123456),1,$time);"; $res = $this->insert_func($db,$sql); //修改數(shù)據(jù) $sql = "update `user` set `age` = 46,`status` = 0 where id = 10;"; $res = $this->save_func($db,$sql); //刪除數(shù)據(jù) $sql = "delete from `user` where `id` = 5;"; $res = $this->save_func($db,$sql); //查詢 $sql = "select * from `user`"; $sql = "select * from `user` limit 0,5";//分頁 $sql = "select * from `user` order by `create_time` desc";//排序 $sql = "select count(*) from `user`";//聚合函數(shù):統(tǒng)計 $res = mysqli_query($db,$sql); $list = $this->select_func($db,$sql); echo '<pre>';print_r($list); mysqli_close($db);//關(guān)閉數(shù)據(jù)庫連接 } //查詢方法 function select_func($db,$sql){ $res = mysqli_query($db,$sql); if($res){ while ($item = mysqli_fetch_assoc($res)){ $list[] = $item; } mysqli_free_result($res); } return $list; } //刪除、修改方法 function save_func($db,$sql){ $res = mysqli_query($db,$sql); return $res; } //插入方法 function insert_func($db,$sql){ $res = mysqli_query($db,$sql); if($res){ $res = mysqli_insert_id($db); } return $res; } 查詢的操作比修改和刪除的都復(fù)雜,在實際應(yīng)用中用到的更多的是查詢操作。
?? ???:韋小寶?? ??:2018-12-14 16:02:43
???? ??:寫的很不錯!mysqli連接數(shù)據(jù)庫也是非常常用的方法之一!