[精讚] [會員登入]
909

[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] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔

[phpmyadmin] 缺少 mcrypt 外掛,請檢查 PHP 設定 缺少 mcrypt 外掛,請檢查 PHP 設定,安裝php-mcrypt。

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

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

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

[Smarty5] 如何在樣版中使用php本身的函式 解決 smarty5版後不能直接叫用 php函式的問題

隨機好文

[AS3] 我做的唯一一個Flash As3遊戲UFO INVADSION [AS3] 我做的唯一一個Flash As3遊戲,是第一個也是最後一個,後來就沒再寫as3,不過as3還滿好玩的。

[MAC] 安裝APACHE+PHP OS Darwin OpenIDMac1deAir 15.6.0 Darwin Kernel Version 15.6.

[AS3] 變數型態 基本類型宣告 as3 有下列幾種基本類型:string, int, number, object, boolean, n

檢測頁面載入速度 利用google提供的檢測工具來測試網頁的載入速度

用Nibbler檢視你的網站優缺點 用Nibbler檢視你的網站存取能力、seo、社交媒體和技術