Blogger Information
Blog 44
fans 0
comment 1
visits 30738
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5月24日——thinkphp中原生查询用法
时光记忆的博客
Original
784 people have browsed it

在TP框架中使用原生查询

  1. 导入Db类:use think\Db;

  2. 创建操作语句:$sql

  3. 执行sql语句:查询使用query()函数,执行增删改使用execute()

二、

2.1查询

    $sql = "SELETE * FROM staff;

    $res = Db::query($sql);

    halt($res);

2.2新增

    $sql = "INSERT INTO staff ('name','age',sex','salary') VALUES ('傅红雪',45,0,2343),('江南',45,1,5436');";

    $num = Db::execute($sql);

    return $num?'新增成功'.$num.;条数据' : '新增失败';

2.3更新

    $sql = "UPDATE staff SET salary=:salary,sex=:sex WHERE salary<=:price;

    $num = Db::execute($sql, ['salary'=>[6000,\PDO::PARAM_INT],'sex'=>[0,\PDO::PARAM_INT],'price'=>['2000',                         \PDO::PARAM_INT]]);

    return  $num?'更新成功'.$num.'条数据':'没有数据被更新';

2.4删除

    $sql = "DELETE FROM staff WHERE id=:id;";

    $num = Db::execute($sql,['id'=>[35, \PDO::PARAM_INT]]);

    return $num?'删除了'.$num.'条数据' :  '没有数据被删除';


Correction status:Uncorrected

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