ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Help: Valid SQL Query returning 0 results

February 07, 2012 4:00pm

Subscribe [2]
  • #1 / Feb 07, 2012 4:00pm

    Dyad

    29 posts

    I’ve created a template who’s sole purpose is to look up an {entry_id} which varies depending on the channel.

    The SQL Query which I’m running is very simple and returns the desired results when run in SequelPro but not when run in a browser.

    $author = $this->EE->TMPL->embed_vars['embed:member'];
    $channel = ($this->EE->TMPL->embed_vars['embed:datatype']=="member" ? 12 : 15);
    
    $query = "SELECT entry_id FROM exp_channel_titles WHERE channel_id='".$channel."' AND author_id='".$author."'";
    $results = $this->EE->db->query($query);

    This echo’s out the correct query but returns 0 results.

    ——
    If I hard code the author_id then it returns the desired results:

    $query = "SELECT entry_id FROM exp_channel_titles WHERE channel_id='12' AND author_id='25'";
    $results = $this->EE->db->query($query);

    This echo’s out the the same query as the first one except with the desired result.

     

  • #2 / Feb 08, 2012 2:59pm

    Dan Decker

    7338 posts

    Hi Dyad,

    I think the issue here is that PHP on input is parsed before embed variables, so your query isn’t being populated with meaningful variables. Have you tried setting PHP to output on your template?

    Do you have template debugging turned on? That is really helpful in determining the queries executed as the template is parsed. It can be a clue to parse order issues.

    Cheers!

  • #3 / Feb 08, 2012 3:16pm

    Dyad

    29 posts

    I already tried turning on debugging. The SQL query was correct there as well.

    SELECT entry_id FROM exp_channel_titles WHERE channel_id='12' AND author_id='25'
  • #4 / Feb 12, 2012 9:24am

    Sean C. Smith

    3818 posts

    Hi Dyad,

    Have you tried switching your template settings to use PHP on Output as Dan suggested? Please try that and then get back to us with the result.

    Sean

  • #5 / Feb 13, 2012 9:05am

    Dyad

    29 posts

    Yes. I’ve tried that.

    Like i said the string i’m passing to $this->EE->db->query(); echo’s out fine.

  • #6 / Feb 14, 2012 3:39pm

    Shane Eckert

    7174 posts

    Hello Dyad,

    Is this template an embeded template?

    Have you seen ExpressionEngine’s Parse Order by Low?

    We use this document a lot around here because parse order is so important.

    Because hard-coding the id makes the query works, this really sounds like parse order.

    Looking forward to your response.

    Cheers,

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases