2013-01-24 58 views
4

這裏是我的代碼:如何正確笨使用別名

$this->db->select('course_name AS Course Name,course_desc AS Course Description,display_public AS Display Status',FALSE); 
$this->db->from('courses'); 
$this->db->where('tennant_id',$tennant_id); 
$this->db->order_by('course_name','ASC'); 
$query = $this->db->get(); 

,我得到了一個錯誤:

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 'Name, course_desc AS Course Description, display_public AS Display Status FROM (' at line 1 

SELECT course_name AS Course Name, 
     course_desc AS Course Description, 
     display_public AS Display Status 
FROM (`courses`) WHERE `tennant_id` = '[email protected]' 
ORDER BY `course_name` ASC 

Filename: C:\wamp\www\coursebooking\system\database\DB_driver.php 

Line Number: 330 

回答

13

嘗試

$this->db->select('course_name AS `Course Name`, course_desc AS `Course Description`, display_public AS `Display Status`', FALSE); 

它在你的別名空間正在與你搞混。

UPDATE

我不知道爲什麼你會想,但我看到什麼阻止你寫

$this->db->select("course_name AS `{$variable}`", FALSE); 

(顯示爲簡單起見,只有一個字段)

UPDATE 2

應該是標準字符串轉換s o我不知道爲什麼它不適合你..總是有分裂的字符串...

$this->db->select('course_name AS `' . $variable . '`', FALSE); 
+0

非常感謝你。它現在的工作 – Eli

+0

另一件事情,是否有可能把變量的別名替代? – Eli

+0

查看更新後的帖子。如果您滿意,也請將答案標記爲已接受。 – danneth