Need help to put variable inside query

Discussion in 'PHP' started by tempex, Jul 8, 2012.

  1. #1
    Hello everybody,
    Need a little help to put variable inside query in php

    $term = $_POST["keyword"];
    $query = 'SELECT * FROM XMLTABLE(
        \'db2-fn:xmlcolumn("TERM.TERM")/term\'
        COLUMNS
        "TERM" VARCHAR (5000) PATH \'term\' ,
        "DEFINITION" VARCHAR (5000) PATH \'definition\'
        ) AS T
    Where term like \'%[COLOR=#ff0000][I]how to put variable $term here[/I][/COLOR]%\'  ';
    Code (markup):
    I've tried
    \'%$term%\'
    Code (markup):
    but no success.


    Thank in advance
     
    tempex, Jul 8, 2012 IP
  2. sylph0810

    sylph0810 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi there, try to echo out your $query, do you see the $term appear correctly? %term% should work.
    You can try to put in execute your SQL query in phpmyadmin as well to see if it's correct.
    Good luck!
     
    sylph0810, Jul 9, 2012 IP
  3. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you for the reply.

    I'm sorry, can you give an example how to echo out my $query.
    Thank you.
     
    tempex, Jul 9, 2012 IP
  4. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $query="SELECT * FROM XMLTABLE(
    'db2-fn:xmlcolumn(\"TERM.TERM\")/term'
    COLUMNS
        TERM VARCHAR (5000) PATH 'term' ,
        DEFINITION VARCHAR (5000) PATH 'definition',
        STANDARD VARCHAR (5000) PATH ' @iso_number ',
        VERSION VARCHAR (5000) PATH  '@version '
        ) AS T
    Where term like '%$term%' 
    ";
    Code (markup):
     
    tempex, Jul 9, 2012 IP
  5. sylph0810

    sylph0810 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi there,

    In your current code,

    $term = $_POST["keyword"];
    echo $term; // you can try to echo term here to see if your term is passed through
    
    $query = 'SELECT * FROM XMLTABLE(    \'db2-fn:xmlcolumn("TERM.TERM")/term\'    COLUMNS    "TERM" VARCHAR (5000) PATH \'term\' ,    "DEFINITION" VARCHAR (5000) PATH \'definition\'    ) AS TWhere term like \'%[COLOR=#ff0000][I]term[/I][/COLOR]%\'';
    
    echo $query // read your query here before you execute your query
    Code (markup):
    Did you get any query error? See what you get?
     
    sylph0810, Jul 9, 2012 IP
  6. tempex

    tempex Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the advice,
    already solve the problem.
     
    tempex, Jul 11, 2012 IP