时间:2021-07-01 10:21:17 帮助过:30人阅读
封装:
<?php class mysqlSql{//封装函数 public $db; public function __construct($dbname){//构造函数 $this->db = new PDO("mysql:host=localhost;dbname=".$dbname,'root','root');//数据库连接 } public function zengjia($table,$title,$body){//数据库增加 $time=time();//当前时间 $sql = "INSERT INTO ".$table .' (id,title,body,create_time)'." VALUES (null,'$title','$body',$time)"; $db = $this->db; $row = $db->query($sql); return $row; } public function shanchu($table,$id){//数据库删除 $sql = "DELETE FROM ".$table." WHERE id=".$id; $db = $this->db; $row = $db->exec($sql); return $row; } public function gengxin($table,$title,$body,$id){//数据库更新 $time=time();//当前时间 $sql = "UPDATE ".$table." SET title='$title',body='$body',update_time=$time where id=".$id; $db =$this->db; $row = $db->exec($sql); return $row; } public function chaxunOne($table,$id){//数据库查询单条记录 $sql = "SELECT * FROM ".$table." WHERE id=".$id; $db = $this->db; $sth = $db->query($sql); $row = $sth->fetch(PDO::FETCH_ASSOC); return $row; } public function chaxunAll($table){//数据库查询全部记录 $sql = "SELECT * FROM ".$table; $db = $this->db; $sth = $db->query($sql); while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { $arr[] = $row; } return $arr; } } ?>
调用:
<?php require "sql.php"; $root = new mysqlSql('wanlala_1'); //var_dump($root->chaxunAll('boke_wz')); //var_dump($root->chaxunOne('boke_wz',2)); //var_dump($root->zengjia('boke_wz',"是盛大发售","的飒飒是打算打算撒大大大是实打实的所说的是答案是")); //var_dump($root->shanchu('boke_wz',30)); var_dump($root->gengxin('boke_wz','啊哈哈哈哈哈','尽快尽快尽快和交换机好就好',31)); ?>
以上就是本篇文章的全部内容了,更多相关内容请关注PHP中文网。
相关推荐:
PHP封装curl的简单方法
详解php封装Mysql操作类
以上就是PHP封装数据库增删改查的详细内容,更多请关注Gxl网其它相关文章!