[精讚] [會員登入]
1290

[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] COOKIE的使用 PHP CodeIgniter 3 中COOKIE的使用超簡單

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

[Centos 6& 7] 安裝php-geoip php-geoip是PHP由domain、ip查詢城市國家資料非常強的函式

[PHP] UTF8中取出字串中特定的字數 要將字串,例如資料庫取出的TEXT,取出特定的字數

設定Google analytics API #1 -- Google網站上的設定 讓你的網站能夠存取你的Google analytics上面的資料

[PHP] 使用FTP PHP 上使用 FTP 的寫法

隨機好文

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

[JAVA] JWS, JWT, JWE, JOSE是什麼? [JAVA] JWS, JWT, JWE, JOSE是什麼?非常的複雜,儘量來搞清楚..

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

APACHE的記錄檔格式 LogFormat 語法 在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm

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