Dynamic SQL queries

Discussion in 'MySQL' started by livewirerules, Feb 8, 2008.

  1. #1
    i have a drop down list menu in one of form and when the user selects a category i need to write a sql statement to get the details from the catergory selected.
    Eg:
    When the user selects option 1,the table 1 should be displayed
    if the user selects option 2 the table 2 must be displayed.

    i need to know is there a way that i can pass the option value to the sql query rathe than using if else statemets

    like,
    select * from <option selected>
     
    livewirerules, Feb 8, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In PHP:

    $query = "SELECT * FROM " . mysql_real_escape_string($optionselected);
     
    CreativeClans, Feb 8, 2008 IP
  3. livewirerules

    livewirerules Active Member

    Messages:
    276
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #3
    That doesn't work

    $result = mysql_query("SELECT * FROM members WHERE last=$last AND first=$first");

    i need to make the members dynamic..

    if the user selects 1 load the table 1 and display the content across the complete site
    if the user selects 2,load the table 2 and display the content across the complete site
     
    livewirerules, Feb 8, 2008 IP
  4. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $query = "SELECT * FROM " . mysql_real_escape_string($optionselected) . "WHERE last=$last AND first=$first";
    $result = mysql_query($query);
    
    PHP:
    Why doesn't that work?
     
    CreativeClans, Feb 8, 2008 IP