[精讚] [會員登入]
1205

[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

你可能感興趣的文章

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

[PHP+ci4] codeignitor4 Cache 及 Session 使用 memcached php 的framework codeignitor4 中的cache和session使用memcached

[Codeingitor4] 使用recaptcha v3前後端認證 為了防止機器人說惡意攻擊,我們引入了 recaptcha,此篇結合 php 框架codeignitor4作前後端認證。

[PHP8] 使用autoload autoload+ namespace +use 到了php7之後,namespace和use越來越重要,此篇整理autoload和namespace、use的結合使用。

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

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

隨機好文

[jQuery] textarea 的取值和給值 HTML 的 TEXTAREA 標籤若要用 jquery 取值,不能使用 .text() 或 .html() ,使用 .

[HP DL380G7] 生效啟動第3,4片網卡/開啟或關閉內建的網卡 HP DL380G7 預設第3,4片網卡裝完系統後找不到,難道是壞了?要怎麼辦?

UTF8中文字/全形一覽 快速查詢urf-8的中文字,共計13246中文字(5401常用字+7652罕用字+日文或編號),292全形符號,27半形符號。

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

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