codeIgniter 輸出 sql 語句,用於測試


<?php
/**
 * CodeIgniter 的測試 Model
 *
 * CodeIgniter 輸出 sql 語句代碼是:
 * $this->db->last_query()
 */
class TestModel extends CI_Model
{

    private $table = 'DBTable';

    public function index()
    {
        $year  = $this->input->post('year');
        $month = $this->input->post('month');
        $day   = $this->input->post('day');

        $this->load->database();
        $result = $this->db
                       ->select('*')
                       ->from($this->table)
                       ->where('year', $year)
                       ->where('month', $month)
                       ->where('day', $day)
                       ->order_by('sort', 'DESC')
                       ->get()
                       ->rerult();

        // 檢查 sql 語句代碼                 
        $this->sqlout(__LINE__); 
        
        return $result;
    }

    /* 功能:文件輸出 sql 語句代碼,用於測試
     *
     * 調用方式:在查詢構造器下方輸入如下代碼
     * $this->sqlout();
     * 
     * 生成的文件目錄:\application\models
     * 文件名:ClassName_line_sql.txt
     */
    public function sqlout($line)
    {
      $testSql = $this->db->last_query();
      if(isset($testSql)){
            $sql = json_decode(str_replace('\n', ' ', json_encode($testSql)));
            file_put_contents(__DIR__.'/'.__CLASS__.'_'.$line.'_sql.txt', $sql);
        }
    }
}

 

codeIgniter  跳轉:redirect(‘signIn/index’);

php 換行符:PHP_EOL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM