The content of this article is about the method (code) of using native MySQL statements in TP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Recently I was developing a project in Thinkphp and wanted to use native MySQL statements. I finally tried it and found it particularly helpful for complex query and update operations! Let’s introduce it in detail below!
1. Use the native mysql core
$Model = M(); $result = $Model->query($sql);//查询 $Model->execute($sql_ex);//更新修改删除
2. Specific usage
<?php /** * 一亩地 */ public function dealYimudi(){ $sql = "SELECT * from yimudi y LEFT JOIN yimudi_use yu on y.yimudi_id =yu.yimudi_id WHERE yu.yimudi_id = 12 and yu.use_status in (1,2) and y.use_end_time+86399 < UNIX_TIMESTAMP(now())"; $Model = M(); $result = $Model->query($sql); $yimudi_ids =[]; foreach ($result as $key => $value) { $yimudi_ids[] =$value['id']; } foreach ($result as $key1 => $value1) { if(in_array($value1['id'],$yimudi_ids)){ $str_yimudi_id = $value1['id']; $sql_ex = "update yimudi_use set use_status = 6 where id = '$str_yimudi_id'"; $Model->execute($sql_ex); } } // $result = $Model->query($sql); // dump($result);die; }
You can try it!
Related recommendations:
Thinkphp upload class implements code for uploading images
How to use TP5.1 template loop tags (code)
The above is the detailed content of How to use native MySQL statements in TP (code). For more information, please follow other related articles on the PHP Chinese website!