SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Variable in MySQL query

    Hi everone.

    Having a problem with getting the values from a database.

    What im trying to do is:
    I use variable 'varname' from packaging_items table (values are coressponding to the names of columns in packaging table ... pack01, pack02 .. and so on).
    But in query result1 instead of getting the value of (pack01, pack02 ..) i get the names of columns (pack01, pack02 ..)
    Here is my short code: (ofcourse there is more to it, but this bit is most important)

    PHP Code:
    $result mysql_query("SELECT * FROM packaging_items") or die(mysql_error()); 
    while(
    $row mysql_fetch_array($result)) { 
        
    $data1 $row['varname'];
        
    $name $row['name'];
        
    $price $row['price'];

    $result1=mysql_query("SELECT `$data1` FROM packaging WHERE orderno='$orderno' LIMIT 1") or die(mysql_error());
    while(
    $row1 mysql_fetch_array$result1 )) {
        if ( 
    $data1 == '' ) {} else {
        echo
    " <tr><td>$name</td><td>$data1</td><td>&pound;$price</td><tr>"; }
    }

    Im stuck here, tried some other options .. and only get worst..

    What do i wrong .. if someone can help will be nice.

    Thank you in advance!

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for example when i hardcode the query, results are ok and that is what i want to achive here

    $result1=mysql_query("SELECT pack01 FROM packaging WHERE orderno='$orderno' LIMIT 1") or die(mysql_error());
    while($row1 = mysql_fetch_array( $result1 )) {
    $pack01 = $row1['pack01'];
    echo "$pack01";
    }

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You really should normalize your database IMO.

    To see what the problem in your query is, do an echo of
    "SELECT `$data1` FROM packaging WHERE orderno='$orderno' LIMIT 1"

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it fixed ...

    $result1=mysql_query("SELECT ".$data1." as data1 FROM packaging WHERE orderno='$orderno' LIMIT 1") or die(mysql_error());
    while($row1 = mysql_fetch_array( $result1 )) {
    $data = $row1['data1'];
    if ( $data == '' ) {} else { echo" <tr><td>$name</td><td>$data</td><td>&pound;$price</td><tr>"; }

    }


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
  •