SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php variable as column name in MySQL statement

    User will enter 2 variables in the form: user id (variable 1) and variable 2. user id is the name of 1st column in MySQL db. php has to locate the user id and parameter that belongs to user id under column 'variable 2'. Basically, how can I use php variable as column name in MySQL statement?

    Thanks.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you mean? Do you want to create the table only after the user enters two variables accordingly? If so, you have to let the user to enter the datatype and size of the field too. And other settings too.

    Or you mean you want to insert those entered values into the existing some tables? So will you be more descriptive?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's say db has 4 cols: user id, favorite color, shoe size, hair color. User only has the option of entering 2 variables. 1 has to be user id, 2nd one can be color OR size OR hair color. Depending on what user entered, php should look up user id #, and then locate parameter under variable 2 col (whatever was entered for variable 2: fave color or shoe size or hair color) that belongs to that user id.

    thanks for your help.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Whistler, Canada
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you're looking for?

    $query = "SELECT {$var2} FROM yourtable WHERE userid='{$var1}'";

    Note: databases do not have columns. Databases have tables, tables have columns/fields.

    Hope this helps.

    Cheers,
    Pepe

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $queryziptrack2 mysql_query("SELECT {$letterheight} FROM ziptrack WHERE size='$trackrows'");
    $querzipytrack1 mysql_fetch_array($queryziptrack2);
    echo 
    $querzipytrack1['{$letterheight}']; 
    returns nothing.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Whistler, Canada
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dima View Post
    PHP Code:
    echo $querzipytrack1['{$letterheight}']; 
    returns nothing.
    Check your last line. You're using single quotes in combination with {$var}, which does not work as you need double quotes in order to get {$var} parsed by php. Try
    PHP Code:
    echo $queryzipytrack1[$letterheight]; 
    If that doesn't work either, check if the query itself returns something.
    PHP Code:
    echo '<pre>';
    print_r($queryzipytrack1);
    echo 
    '</pre>'
    Hope this helps.

    Cheers,
    Pepe

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys!

    PHP Code:
    echo $querzipytrack1["{$letterheight}"]; 
    made all the difference.

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now, I wanted to ask the another question which is the extension of everything mentioned above.

    I have a form filled with colors with checkboxes next to them. When user checks off as many colors as he wants. PHP should receive this variables and after pairing them up with each one another should check the table in db for compatibility.

    1st field name of the table in the db is called 'color', the following field names consists of actual color names (black, white, etc). 1st field name 'color' has a record names of colors (black, white, etc) that match field names. I filled this table with 0s and 1s (1 for compatibility). I have stopped here. How I should proceed next?

    Appreciate your advice.

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your field name is the same as the form element name, then IP-Dope's sql query should work with the some addition. I think you should have an sql like this:
    PHP Code:
    $sql "SELECT * FROM tablename WHERE userid='$userid_variable' AND (fav_color='$variable2' OR shoue_size='$variable2' OR haircolor='$variable2')"
    Does that work for you now?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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