Blogger Information
Blog 6
fans 0
comment 0
visits 7623
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
insert/update/delete/select应用
小淇的博客
Original
571 people have browsed it

 1. 写出常用的CURD语句的基本语法;

插入语句:INSERT INTO `数据表` (`字段1`,`字段2`...) VALUES ('值1','值2'...)

更新语句:UPDATE `数据表` SET `字段1`='值1', `字段2`='值2'... WHERE  更新条件

查询语句:SELECT `字段列表` FROM `数据表名` WHERE 查询条件`

删除语句:DELETE FROM `数据表`  WHERE  删除条件

2.PDO中操作数据表的基本步骤, 并实例演示insert/update/delete/select

pdo作步骤:连接数据库->创建构sql语名模板->创建预处理对象->变量绑定->执行sql语句->关闭语句

实例

<?php
class pdodb{
private $config=[
'type' => 'mysql',
'host' => '127.0.0.1',
'dbname' => 'cs',
'username' => 'root',
'password' => 'root'
];

    public function construct($status=1)
    {


     $dsn = "{$this->config['type']}:host={$this->config['host']};dbname={$this->config['dbname']};";
        $username ="{$this->config['username']}";
        $password ="{$this->config['password']}";
        try {
            $pdo = new PDO($dsn, $username, $password);

        } catch (PDOException $e) {
            die('连接失败' . $e->getMessage());
        }
        if ($status ===Null){
            $pdo = null;
        }else{
            return $pdo;
        }}



//查询
    public function query($sql='',$persql=''){

        $stmt=$this->construct()->prepare($sql);
        $stmt->execute($persql);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $rows;

    }
   /*更新-写入-删除*/
    public function insert($sql='',$persql=''){
        $stmt=$this->construct()->prepare($sql);
        $stmt->execute($persql);
        return $stmt;
    }
    //关闭数据库
    public function close() {
return $this->construct($status=null);
    }



/*$dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
$username = $db['username'];
$password = $db['password'];
try {
    $pdo = new PDO($dsn, $username, $password);
  var_dump($pdo);
} catch (PDOException $e) {
    die('连接失败' . $e->getMessage());
}

*/

}

$dba=new pdodb();
//查询user_id用户信息
$sql='select * from name where user_id=:user_id';
$persql=['user_id'=> 2];

$users=$dba->query($sql,$persql);
foreach ($users as $user) {
    echo '<pre>' .$user['name'].'-'.$user['room'];   //print_r($user, true);
}
$dba->close();
//更新一条信息
$updatasql='UPDATE `name` SET `name`= :name, `sex`= :sex, `room`= :room WHERE `user_id`=:user_id';
$persql=['name'=>'李三梅','sex'=>'2','room'=>'第三教室','user_id'=>2];
$user=$dba->insert($updatasql,$persql);
echo '成功的更新' . $user->rowCount(). '条记录';
$dba->close();
//增加一条信息
$insertsql='INSERT INTO `name` SET `name`= :name, `sex`= :sex,`room`= :room';
$persql=['name'=>'张天丰','sex'=>'1','room'=>'第一教室'];
$user=$dba->insert($insertsql,$persql);
echo '成功的增加' . $user->rowCount(). '条记录';
$dba->close();
//删除一条信息
$deletesql='delete from `name` where `user_id`=:user_id';
$persql=['user_id'=>'1'];
$user=$dba->insert($deletesql,$persql);
echo '成功删除' . $user->rowCount(). '条记录';
$dba->close();?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

运行后结果

QQ图片20190728114543.png

Correction status:unqualified

Teacher's comments:请用课堂上的知识完成作业,类与对象还没有学习到, 可以事先预习这是推荐的好习惯,但作业要按规则来完成....
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post