码迷,mamicode.com
首页 > 数据库 > 详细

用thinkphp执行原生sql

时间:2016-10-17 00:26:56      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

Controller代码:

Demo2Controller.class.php

<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;

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>

 

用thinkphp执行原生sql

标签:

原文地址:http://www.cnblogs.com/modou/p/5968154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!