如何正确使用笨别名(How to properly use Alias in Codeigniter

2019-07-18 16:01发布

这里是我的代码:

$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

我得到了一个错误:

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` = 'elicuarto@apploma.com' 
ORDER    BY `course_name` ASC

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

Line Number: 330

Answer 1:

尝试

$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);

(示出为简单起见只是一个场)

更新2

应该是标准的字符串转换,所以我不知道为什么它不为你工作..总有分裂串...

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


文章来源: How to properly use Alias in Codeigniter
标签: codeigniter