zoukankan      html  css  js  c++  java
  • 用thinkphp执行原生sql

    Controller代码:

    Demo2Controller.class.php

    <?php
    namespace HomeController;
    use ThinkController;
    use ThinkModel;
    
    class Demo2Controller extends Controller {
        
        //insert 操作
        public function test1(){
            $Model = new Model();
            $sql = "insert into city(cityname,province,citydesc) values('石家庄','河北省','河北省城市')";
            $Model->execute($sql);
            
            echo "insert 操作";
        }
        
        //delete 操作
        public function test2(){
            $Model = new Model();
            $sql = "delete from city where cityname='石家庄'";
            $Model->execute($sql);
             
            echo "delete 操作";
        }
        
        // update 操作
        public function test3(){
            $Model = new Model();
            $sql = "update city set citydesc='河北省省会' where cityname='石家庄'";
            $Model->execute($sql);
        
            echo "update 操作";
        }
        
        // select 操作
        public function test4(){
            $Model = new Model();
            $sql = "select * from city order by id asc";
            $list = $Model->query($sql);
            
            $this->assign('list',$list);
            $this->display();
        }
    
    }

    select 操作对应的View页面:

    test4.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>test3</title>
    </head>
    <body>
    <table width="100%" border="1" cellspacing="0" cellpadding="0">
      <tr>
          <td>序号</td>
        <td>城市</td>
        <td>省会</td>
        <td>描述</td>
      </tr>
      <foreach name="list" item="item" key="index">
      <tr>
          <td>{$index+1}</td>
        <td>{$item.cityname}</td>
        <td>{$item.province}</td>
        <td>{$item.citydesc}</td>
      </tr>
      </foreach>
    </table>
    </body>
    </html>

    执行原生sql没有找到可以传参的方法,如果需要传参,我是这么处理的

            $Model = new Model();
            $sql = sprintf("delete from tbrecord where recordid=%d",I('get.id'));
            $Model->execute($sql);

    使用了sprintf,想了解sprintf的用法,请自行百度

  • 相关阅读:
    BFC
    js异常处理
    vue双向数据绑定的简单实现
    cookie封装,localstage封装
    问题 1476: [蓝桥杯][基础练习VIP]龟兔赛跑预测 (模拟)
    HDU 6205 (模拟) card card card
    HDU 4545 (模拟) 魔法串
    HDU 4521 小明系列问题——小明序列 (线段树 单点更新)
    基础动态规划 讲解
    HDU 1561 The more, The Better (有依赖背包 || 树形DP)
  • 原文地址:https://www.cnblogs.com/modou/p/5968154.html
Copyright © 2011-2022 走看看