hard-coded queries

edited February 2014 in Modules
I wanted to use the query() function on one of my models.
$q = "SELECT * FROM `mytable`;"; /* Actual production query is way more complicated than this example. I'm not sure it could even be built using active record*/ $query = $this->db->query($q); $r = $query->result_array();
My result set is empty even though I know the data exists. Is this some sort of Fuel/Active record issue?

Comments

  • edited 5:05AM
    FUEL uses active record and those methods on the DB object should work.
  • Indeed. I use such queries in vanilla CodeIgniter all the time. In fact, the simple SELECT query in the example works in Fuel.

    In practice, using my complex query, adding $this->db->last_query() gives me the query I submitted ($q) and running that query directly on the database elicits results. I just get a blank array when I run it within Fuel :(

    My query:
    SELECT DISTINCT IF(lev1rank = 7,lev1id,IF(lev2rank = 7,lev2id,IF(lev3rank = 7,lev3id,IF(lev4rank = 7,lev4id,'')))) AS `ID`, IF(lev1rank = 7,lev1title,IF(lev2rank = 7,lev2title,IF(lev3rank = 7,lev3title,IF(lev4rank = 7,lev4title,'')))) AS `Name`, IF(lev1rank = 7,lev1rank,IF(lev2rank = 7,lev2rank,IF(lev3rank = 7,lev3rank,IF(lev4rank = 7,lev4rank,'')))) AS `RankID`, synonymCommonName FROM (SELECT t1.ID AS lev1id, t1.Name AS lev1title, t1.RankID AS lev1rank, t2.ID AS lev2id, t2.Name AS lev2title, t2.RankID AS lev2rank, t3.ID AS lev3id, t3.Name AS lev3title, t3.RankID AS lev3rank, t4.ID AS lev4id, t4.Name AS lev4title, t4.RankID AS lev4rank FROM mytable AS t1 LEFT JOIN mytable AS t2 ON t2.ParentID = t1.ID LEFT JOIN mytable AS t3 ON t3.ParentID = t2.ID LEFT JOIN mytable AS t4 ON t4.ParentID = t3.ID WHERE t1.ID = 2 ) AS lookup JOIN resolve_mytable_synonyms ON resolve_mytable_synonyms.ID = IF(lev1rank = @rank,lev1id,IF(lev2rank = @rank,lev2id,IF(lev3rank = @rank,lev3id,IF(lev4rank = @rank,lev4id,'')))) ORDER BY `Name`;

    Could it be a timeout issue?
  • Marvelous! Your Forum code block prettifying has given me the clue the "@rank"s are at fault.

    Weird that the query renders correctly for ::last_query() and so works when input outside Fuel.

    I guess I would have found this quicker with some query debugging & errors via CI/Fuel. How's this done in Fuel usually?
Sign In or Register to comment.