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.

SQL Query in a parameter

September 20, 2012 11:43am

Subscribe [1]
  • #1 / Sep 20, 2012 11:43am

    T Adams

    36 posts

    Hello,

    I am using this sql query to find the member_id from the screen_name

    {exp:query sql="SELECT member_id FROM exp_members WHERE screen_name = 'Admin' "}
     {member_id}
    {/exp:query}

    and it works perfectly. The problem I am having is trying to assign the now retrieved member_id to an {exp..} parameter.

    I have the query in a snippet but if try and assign the snippet to a parameter, the parameter pulls the whole code from the snippet and not just the member_id, which causes my template code to fail.

    Is there a way of assigning the retrieved member ID to a variable which I can then use in the {exp…} tags or a way of getting expression engine to parse the snippet fully before trying to assign it to the parameter?

    I hope that explanation made sense :(

    T.

  • #2 / Sep 24, 2012 1:27am

    Kevin Smith

    4784 posts

    Hi T,

    You just need to surround the {exp…} tag with the whole Query module tag, using only the {member_id} variable inside the {exp…} tag’s parameter. Right now you’re using the whole Query module tag inside the parameter, so it’s not being parsed in time.

    By the way, I like to keep this PDF close by as a handy reference.

    Does that recommended change help?

  • #3 / Sep 24, 2012 7:48pm

    T Adams

    36 posts

    Thanks a lot, that worked perfectly. Because I will be using the generated member_id multiple times within the template, will it be better to wrap the entire template in the Query Module tag and just call the member_id wherever I want it, or will this impact the template adversely?

    T.

  • #4 / Sep 25, 2012 4:00pm

    Kevin Smith

    4784 posts

    I don’t think that’ll adversely affect performance (though you could run some benchmarks to see), but for easily readability in your template code, I’d recommend using something like Stash to store the member_id as a variable that you can use later in the template.

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

ExpressionEngine News!

#eecms, #events, #releases