+ Reply to Thread
Results 1 to 8 of 8

Thread: MySQL Extension and Variables

  1. #1

    Default MySQL Extension and Variables

    Hi guys,

    I'm testing out the MySQL extension and I'm just wondering if there's a way to use a variable in the query section. For example, I want to use {{page.title}} in the where clause of the query, how do I do this?

    I tried something like {{ mysql.value("select name from users where username = '{{ page.title }}'") }} but I get an empty resultset. I'm not even sure if this is the correct syntax.

    Thanks in advance.

  2. #2
    Join Date
    Feb 2008
    Location
    London upon Thames
    Posts
    2,792

    Default

    Concatenation

    Something like
    Code:
    {{ mysql.value("select name from users where username = " .. page.title .. ";") }}
    perhaps?

  3. #3

    Default

    That worked! I just had to add additional single quotes to the code you gave me so it looks like this:
    {{ mysql.value("select name from users where username = '" .. page.title .. "'") }}

    Thanks a lot! You saved me a lot of time man, I would've never figured that out .

  4. #4
    Join Date
    Feb 2008
    Location
    London upon Thames
    Posts
    2,792

    Default

    No problem. I had to RTFM to find it, but I just knew where to find the manual

  5. #5
    Join Date
    Jul 2006
    Location
    San Diego, CA
    Posts
    5,450

    Default

    Make sure to use the string.sqlescape function to avoid SQL injection problems:
    Code:
    {{ mysql.value("select name from users where username = '" .. string.sqlescape(page.title) .. "'") }}
    Steve G. Bjorg - Chief Architect
    Did you check the MindTouch FAQ?
    Found a bug? Report it.
    Follow me on Twitter
    Find us on IRC: irc.freenode.net #mindtouch

  6. #6
    Join Date
    Aug 2007
    Posts
    66

    Default

    Hi Steve,
    Can i use simple page level variables to parameterize the query string ?
    I know that mysql extension doesnt support pubsub but i just want to do something like this

    var fname="pratheesh"
    var lname="kumar"

    mysql.table("select * from user where first_name='" .. fname .. "' and last_name = '" ..lname .. "'")

    is this doable ? I'm trying out various combinations. If i get it working. I'll post it here.

  7. #7
    Join Date
    Aug 2007
    Posts
    66

    Default

    Not sure if this is the best way,
    but this works

    {{ var mylist=["2142"];
    payx.table("select first_name from user where user_id =" ..mylist[0]..";")
    }}

  8. #8
    Join Date
    Jul 2006
    Location
    San Diego, CA
    Posts
    5,450

    Default

    Yes, you can use anything you like as a variable as long as it is defined.
    Steve G. Bjorg - Chief Architect
    Did you check the MindTouch FAQ?
    Found a bug? Report it.
    Follow me on Twitter
    Find us on IRC: irc.freenode.net #mindtouch

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts