[精讚] [會員登入]
1147

[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

你可能感興趣的文章

PHP 移除陣列中的元素 要移除陣列中的其中一項元素

[PHP] codeignitor4+ smarty4 這篇整合 php 的framework codeignitor4 + smarty4。

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

PHP程式經驗 #2 -- print和echo的差異 常在寫php的人一定會想知道echo和print這兩個函數有什麼不一樣 驗證 1. 比較print 和echo 函式的執

[PHP] 讀取作業系統程式執行結果 PHP讀取作業系統程式執行結果

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

隨機好文

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

魔球中小女孩唱的歌 The show 魔球中小女孩唱的歌 The show

[PHP] 檢查IP是否在某個網段內 mtachcidr 要檢查IP是否在某個網段內,要寫幾行?10行?5行? 不用,只要2行。以下是我寫的 code /** * matchCI

問問題 問問題其實內涵很深,我悟了很久才懂。 有人問題的目的並不一定是想要得到答案,有時只是純粹想問問題..

此一時彼一時 我是不是易怒的人,其實我也不知道,總之我常會失控,不知道自己在幹嘛。這近生活過得浮浮的,不是很踏實,總會想太多,我甚至會