Home >Backend Development >PHP Tutorial >How to use native MySQL statements in TP (code)

How to use native MySQL statements in TP (code)

不言
不言Original
2018-08-20 17:22:132854browse

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[&#39;id&#39;];
        }
        foreach ($result as $key1 => $value1) {
           if(in_array($value1[&#39;id&#39;],$yimudi_ids)){
            $str_yimudi_id = $value1[&#39;id&#39;];
            $sql_ex = "update yimudi_use set use_status = 6 where id = &#39;$str_yimudi_id&#39;";

              $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn