CodeIgniter Forums
Problem with active records - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Problem with active records (/showthread.php?tid=31237)



Problem with active records - El Forum - 06-10-2010

[eluser]Unknown[/eluser]
Hello! I've just started using CodeIgniter and found it surprisingly easy to use (I switched from ZF). Now I have ran into my first problem, which has to do with Active records (which I haven't used before)

I have a function that looks like this:
Code:
function get_top_rated_movies($limit_num_of_movies) {
            $this->db->select("title, TRUNCATE((rating_acting+rating_dvd+rating_overall)/3, 1) as avg_rating");
            $this->db->order_by("avg_rating", "DESC");
            $this->db->limit($limit_num_of_movies);
            $query = $this->db->get();

            return $query;
        }

This will return an error:
Quote: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 'as avg_rating FROM (`dvd_collection`) ORDER BY `avg_rating` DESC LIMIT 20' at line 1

SELECT `title`, TRUNCATE((rating_acting+rating_dvd+rating_overall)/3, `1)` as avg_rating FROM (`dvd_collection`) ORDER BY `avg_rating` DESC LIMIT 20

For some reason it encloses 1) with singular quotation-marks. Is there something I have missed here? It's seems very odd to me.

(Sorry if my language seems to be a bit off, I'm from Sweden)


Problem with active records - El Forum - 06-11-2010

[eluser]WanWizard[/eluser]
Use the second parameter of $this->db->select() to prevent escaping. The select method escape functionality can't handle much more than just a list a fields...


Problem with active records - El Forum - 06-11-2010

[eluser]Unknown[/eluser]
That worked. Thanks!