2011-08-09 114 views
2

CodeIgniter活動記錄很容易,有據可查,功能強大。但是當我嘗試插入函數CONCAT,NOW,GROUP_CONCAT,, DATEDIFF,TRIM等內建的MySQL或我的自定義函數時,它會給出錯誤。下面的代碼工作正常...MySQL函數和CodeIgniter活動記錄

$result = $this->db->select('p.first_name, p.last_name, p.mobile_number, p.email_address')->from('profile p')->get()->result(); 

但是,當我想聯繫first_namelast_name和使用MySQL CONCAT功能是這樣的...

$result = $this->db->select('CONCAT(p.first_name, " ", p.last_name) fullname, p.mobile_number, p.email_address')->from('profile p')->get()->result(); 

它顯示數據庫錯誤

A Database Error Occurred 

Error Number: 1064 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '", `p`.`last_name)` fullname, `p`.`mobile_number`, `p`.`email_address` FROM (`pr' at line 1 

SELECT CONCAT(p.first_name, `"` ", `p`.`last_name)` fullname, `p`.`mobile_number`, `p`.`email_address` FROM (`profile` p) 

Filename: D:\xampp\htdocs\example\system\database\DB_driver.php 

Line Number: 330 

有什麼方法可以在CodeIgniter Active Record中插入MySQL函數嗎?希望我清楚。提前致謝。

+0

可能重複:[CONCAT()在選擇欄列表功能(http://stackoverflow.com/q/4519838)和示例:CONCAT在PHP笨( http://stackoverflow.com/q/4623958)。 – Apostle

回答

0
  • 解決了該問題

在有源記錄陣列加入的字段名可以解決這個問題。在數組裏你可以使用任何mysql函數。上面的修復。

MySQL表:

fullname    mobile_number email_address     
------------------- ------------- ------------------------------ 
sitaramaiah javvadi 9989403339  [email protected] 
raja kumar guthula 9949526012  [email protected]  
chandra sekhar k.l 9912144556  [email protected]  
khadar basha  98884884584 [email protected]   
super administrator 9841866445  [email protected] 

笨的Active Record:

$result = $this->db->select(array('CONCAT(p.first_name, " ", p.last_name) `fullname`', 'p.mobile_number', 'p.email_address'))->from('profile `p`')->get()->result(); 
echo '<pre>'; 
print_r($result); 

輸出:

Array 
(
    [0] => stdClass Object 
     (
      [fullname] => sitaramaiah javvadi 
      [mobile_number] => 9989403339 
      [email_address] => [email protected] 
     ) 

    [1] => stdClass Object 
     (
      [fullname] => raja kumar guthula 
      [mobile_number] => 9949526012 
      [email_address] => [email protected]nline.in 
     ) 

    [2] => stdClass Object 
     (
      [fullname] => chandra sekhar k.l 
      [mobile_number] => 9912144556 
      [email_address] => [email protected] 
     ) 

    [3] => stdClass Object 
     (
      [fullname] => khadar basha 
      [mobile_number] => 98884884584 
      [email_address] => [email protected] 
     ) 

    [4] => stdClass Object 
     (
      [fullname] => super administrator 
      [mobile_number] => 9841866445 
      [email_address] => [email protected] 
     ) 

) 
4

因爲我不知道你的確切的錯誤:

從user_guide:

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

$result = $this->db->select('CONCAT(p.first_name, " ", p.last_name) fullname, p.mobile_number, p.email_address', FALSE)->from('profile p')->get()->result(); 
+2

@Madan我傾向於同意Bernhard - 如果你仔細看看你的錯誤,你會注意到''''在'''之前和CONCAT方法完成之後的額外'\'',這似乎是問題所在。 – cwallenpoole

+0

@Madan您的選擇是使用FALSE或不使用'select'方法 – cwallenpoole

1

放置在陣列 $這個 - > DB->選擇(排列你 「選擇」( 'CONCAT(p.first_name,「」,p.last_name)全名,p.mobile_number,p.email_address'))

通過。

1

對於使用活動記錄更新的情況下,你可以使用

$this->db->set("date_read", "NOW()", FALSE);

將輸出類似這樣

`SET `date_read` = NOW(),
+0

@Goodluck,我知道'FALSE'參數,我在搜索時沒有'FALSE',我也已經更新了答案。 –

+1

'FALSE在這裏需要'''''',否則你不想被轉義的東西將被轉義。因此,避免在此處注入「動態」(user-input = untrusted)數據('$ someFooPostData')會導致SQJ注入攻擊。 **請務必**謹慎使用。 在這裏,我用基於Active-Record-Pattern的方法更新SQL函數列確實有效。 – Roland