[精讚] [會員登入]
1103

[CodeIgniter 3] 修改或插入資料時遇到函數的處理

CI3 中要新增或修改的資料中如果有 now()這類的函數,要怎麼處理?

分享此文連結 //n.sfs.tw/13128

分享連結 [CodeIgniter 3] 修改或插入資料時遇到函數的處理@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 12:44:19 最後編修
2019-02-26 01:55:32 By 張○○
 

CodeIgniter 3如果要用db helper來組合字串的話,遇到sql中有函數或是需要運算的話,就得使用set函數

否則預設會把該函數變成「字串」。

舉例:

update table set st=1, startDT=now(), cnt=cnt+1, dueDT=date_add(now(),interval 30 day where sn=123;

 在 set 的函數中要代入第三個變數 false,db helper就會忽略檢查,直接變成函數處理。

$row= array( 
  'st' => 1
);
$this->db->set('startDT', 'now()', false);
$this->db->set('cnt', 'cnt+1', false);
$this->db->set('dueDT', "date_add(now(),interval 30 day)", false);
$ok= $this->db->where('sn',123)->limit(1)->update('table', $row);

第7行指定limit(1)是為了確保寫錯SQL只會影響1行。

 

參考資料

[1] https://stackoverflow.com/questions/16440366/how-to-get-last-insert-id-after-insert-query-in-codeigniter-active-record

END

你可能感興趣的文章

[CodeIgniter 3] 資料庫的使用方法整理1/2 --Select的使用 [CodeIgniter 3] 資料庫的使用方法整理:Select的使用

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

設定Google analytics API #3 -- 查詢範例 讓你的網站能夠存取你的Google analytics上面的資料

[CodeIgniter 3] 修改或插入資料時遇到函數的處理 CI3 中要新增或修改的資料中如果有 now()這類的函數,要怎麼處理?

[PHP] 類別中要怎麼使用callback function 召回函數(回呼函數)? 在類別中使用標準函數,若其中的callback 函數也是在類別中,該怎麼使用?

作業上傳程式 提供學生作業上傳的程式

隨機好文

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

[Windows7] 移除IE10及移除IE11 Windows7 不得已的情況要移除IE11或IE10怎麼做?

[札記] 2016.7~12月札記 札記,只是札記

網頁的標頭(head)搜尋引擎和FB最佳化 網頁的標頭(head)搜尋引擎和facebook最佳化