CodeIgniter icon indicating copy to clipboard operation
CodeIgniter copied to clipboard

Active Record - from() and alias escaping (MySQL)

Open joellimberg opened this issue 14 years ago • 7 comments

$this->db->select('update.*, user.*');

$this->db->where('user.id = update.user_id', null);

$this->db->from('issue_updates AS update');
$this->db->from('users AS user');

$query = $this->db->get();

This results in the query:

SELECT `update`.*, `user`.* FROM (`issue_updates` AS update, `users` AS user) WHERE `user`.`id` = update.user_id

Resulting error:

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 'update, users AS user) WHERE user.id = update.user_id' at line 2

Apparently the table name alias is not escaped, so "update" causes an error.

joellimberg avatar May 23 '12 22:05 joellimberg

Is this with CI 2.1 or the develop branch code?

narfbg avatar May 23 '12 22:05 narfbg

This is CI 2.1.0.

(Sorry, forgot.)

joellimberg avatar May 23 '12 22:05 joellimberg

Turns out both are affected anyway, I'll look into it probably tomorrow.

narfbg avatar May 23 '12 22:05 narfbg

@joellimberg See the referenced commit for the fix.

narfbg avatar May 24 '12 16:05 narfbg

Thanks, works perfectly.

I wanted to comment about there being a bug about _escape_identifiers() vs escape_identifiers(), but this is just because I'm using 2.1.0, and only merged the few changed lines.

joellimberg avatar May 27 '12 12:05 joellimberg

Well, it is a bug - escape_identifiers() doesn't exist in the 2.1-stable branch. Fixed in the last commit, you can just put the underscore in front of it. :)

narfbg avatar May 27 '12 13:05 narfbg

This fix broke session deletion when using the mysql db backend with CI 2.1.1:

DELETE FROM `ci_sessions` WHERE `last_activity` `< 1339434355`

Notice that the quote is before the < instead of after it.

Bluewind avatar Jun 13 '12 17:06 Bluewind