Execute native SQL with thinkphp

Source: Internet
Author: User

Controller code:

Demo2Controller.class.php

<?phpnamespace Home\controller; UseThink\controller; UseThink\model;classDemo2controllerextendsController {//Insert Operation     Public functiontest1 () {$Model=NewModel (); $sql= "INSERT into city (CITYNAME,PROVINCE,CITYDESC) VALUES (' Shijiazhuang ', ' Hebei province ', ' Hebei province ')"; $Model->execute ($sql); EchoThe Insert Action; }        //Delete Operation     Public functiontest2 () {$Model=NewModel (); $sql= "Delete from city where cityname= ' Shijiazhuang '"; $Model->execute ($sql); Echo"Delete Action"; }        //Update Operation     Public functiontest3 () {$Model=NewModel (); $sql= "Update City set citydesc= ' capital of Hebei province ' where Cityname= ' Shijiazhuang '"; $Model->execute ($sql); Echo"Update Action"; }        //Select Operation     Public functiontest4 () {$Model=NewModel (); $sql= "SELECT * from City ORDER by ID ASC"; $list=$Model->query ($sql); $this->assign (' list ',$list); $this-display (); }}

The view page for the Select operation:

Test4.html

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "UTF-8"><title>Test3</title></Head><Body><Tablewidth= "100%"Border= "1"cellspacing= "0"cellpadding= "0">  <TR>      <TD>Serial number</TD>    <TD>City</TD>    <TD>Capital</TD>    <TD>Describe</TD>  </TR>  <foreachname= "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>

Execute native SQL with thinkphp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.