Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use Alias in Codeigniter

Tags:

codeigniter

Here is my code:

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

and I got an error:

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

and I got an error:

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
like image 964
Eli Avatar asked Jan 24 '13 11:01

Eli


1 Answers

Try

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

It's the space in your alias that is messing with you.

UPDATE

I'm not sure why you would want to, but I see nothing preventing you from writing

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

(showing just one field for simplicity)

UPDATE 2

Should be standard string conversion so I don't know why it doesn't work for you.. there's always split strings...

$this->db->select('course_name AS `' . $variable . '`', FALSE);
like image 111
danneth Avatar answered Sep 21 '22 02:09

danneth