Rishi Reddy Rishi Reddy - 4 months ago 7
SQL Question

inserting a mysql query in codeigniter

$query = $this->db->query('select
sum(like) as atotal
from
like
where
sfid = '.$short);<br>
print_r($query);


and the error is


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 'like) as atotal from like where sfid = 11' at line 2


select sum(like) as atotal from like where sfid = 11


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


Line Number: 330


'like' in sum(like) is column name and 'like' after 'from' is table name
thank you in advance

Answer

You shouldn't use like keyword in your query. It is sql-specific word treated as special keyword. To solve this task, you should change column name in database table or wrap like keyword in backticks either:

$query = $this->db->query('select sum(`like`) as atotal from `like` where sfid = '.$short);
Comments