thinkphp對mysql的CURD操作

來源:互聯網
上載者:User

標籤:

利用thinkphp(3.2.3)來操作資料庫,首先要串連資料庫。我們需要對某資料庫寫一個設定檔,thinkphp會根據該設定檔自動連接上資料庫。而model檔案就不用自訂,內建的即可解決問題。設定檔寫於目錄application\home\conf\config.php下:

<?phpreturn array(//‘配置項‘=>‘配置值‘‘DB_TYPE‘=> ‘mysql‘,//資料庫類型‘DB_HOST‘=> ‘127.0.0.1‘,//伺服器位址‘DB_NAME‘=>‘jiu151231‘,//資料庫名稱‘DB_USER‘=>‘root‘,//使用者名稱‘DB_PWD‘=>‘‘,//密碼‘DB_PORT‘=>‘3306‘,//連接埠‘DB_PREFIX‘=>‘‘,//資料庫表首碼);

到此,準備工作完成。其中,那個資料庫表首碼,網上關於預設各種說法,我的預設為空白。估計其他首碼,開啟phpmyadmin可以看到吧。

CURD作業碼都在目錄application\home\controller\indexcontroller.class.php裡,這是控制器檔案。下面都是採用M方法執行個體化表對象的。

C

插入資料

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $jiu0=M(‘myguests‘);        $j0[‘firstname‘]=‘lv‘;        $j0[‘lastname‘]=‘oe‘;        $jiu0->add($j0);    }}

插入多條資料

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $jiu0=M(‘myguests‘);        $list[]=array(‘firstname‘=>‘li‘,‘lastname‘=>‘weifeng‘);        $list[]=array(‘firstname‘=>‘liu‘,‘lastname‘=>‘tian‘);        $jiu0->addAll($list);    }}

R

讀單條記錄 find方法返回第一個合格記錄

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu=M(‘myguests‘);        $data=$gu->where(‘firstname="lv"‘)->find();        dump($data);    }}
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu=M(‘myguests‘);        $gu->where(‘firstname="lv"‘)->find();        dump($gu->data());    }}

讀多條資料 select方法 如果查詢出錯,select的傳回值是false,如果查詢結果為空白,則返回NULL,否則返回二維數組。

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $list = $gu->where(‘firstname="lv"‘)->order(‘id‘)->limit(5)->select();//返回5條資料        dump($list);    }}

 讀取欄位值

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $list = $gu->where(‘id=5‘)->getField(‘firstname‘);        dump($list);    }}
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $list = $gu->getField(‘id‘,true);//返回整列        dump($list);    }}
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $list = $gu->getField(‘id,firstname‘,5);//返回多列,5條記錄        dump($list);    }}

U

save方法

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $data[‘firstname‘]=‘liu‘;        $data[‘lastname‘]=‘hl‘;        $gu->where(‘id=5‘)->save($data);    }}
<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $gu->firstname=‘liu‘;        $gu->lastname=‘hl‘;        $gu->where(‘id=6‘)->save();    }}

更新欄位

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $gu->where(‘id=6‘)->setField(‘firstname‘,‘wang‘);    }}

更新多個欄位

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $data=array(‘firstname‘=>‘lv‘,‘lastname‘=>‘huan‘);        $gu->where(‘id=1‘)->setField($data);    }}

而對於統計欄位(通常指的是數字類型)的更新,系統還提供了 setInc 和 setDec 方法。

$User = M("User"); // 執行個體化User對象$User->where(‘id=5‘)->setInc(‘score‘,3); // 使用者的積分加3$User->where(‘id=5‘)->setInc(‘score‘); // 使用者的積分加1$User->where(‘id=5‘)->setDec(‘score‘,5); // 使用者的積分減5$User->where(‘id=5‘)->setDec(‘score‘); // 使用者的積分減1

3.2.3版本開始,setInc和setDec方法支援延遲更新,用法如下:

$Article = M("Article"); // 執行個體化Article對象$Article->where(‘id=5‘)->setInc(‘view‘,1); // 文章閱讀數加1$Article->where(‘id=5‘)->setInc(‘view‘,1,60); // 文章閱讀數加1,並且延遲60秒更新(寫入)

D

delete方法

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {    public function index(){        $gu = M("myguests");        $gu->delete(2);//刪除主鍵2對應的記錄    }}

delete方法可以刪除單個資料,也可以刪除多個資料,這取決於刪除條件,例如:

$User = M("User"); // 執行個體化User對象$User->where(‘id=5‘)->delete(); // 刪除id為5的使用者資料$User->delete(‘1,2,5‘); // 刪除主鍵為1,2和5的使用者資料$User->where(‘firstname="lv"‘)->delete(); // 刪除所有firstname為"lv"的使用者資料

 



thinkphp對mysql的CURD操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.