www.webdeveloper.com
Results 1 to 13 of 13

Thread: Using Variable in MySQL query

  1. #1
    Join Date
    Jun 2007
    Posts
    25

    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
    Join Date
    Aug 2012
    Posts
    155
    Try echoing out your $result1 query (you will need to remove the mysql_query wrapper first) and then run what is return directly in PHPMyAdmin to see what happens (easier to debug sometimes).

  3. #3
    Join Date
    Jun 2007
    Posts
    25
    well i did ...

    pack01pack02pack03pack04pack05pack06pack07 just like i mentioned before, these are names of columns in packaging table, and frome these i want to get the values out..
    need to have another variable specified for that ...and i jus do not now how to do this ..

  4. #4
    Join Date
    Jun 2007
    Posts
    25
    well i did ...

    pack01pack02pack03pack04pack05pack06pack07 just like i mentioned before, these are names of columns in packaging table, and from these i want to get the values out..
    need to have another variable specified for that ...and i just do not now how to do this ..

  5. #5
    Join Date
    Aug 2012
    Posts
    155
    So when you ran your query in phpmyadmin you got no errors and then just a list of column names? Are you absolutely sure these are column names?

  6. #6
    Join Date
    Jun 2007
    Posts
    25
    that is for sure, values coming from the varname variable in packaging_items table , are the names exactlly the same as columns names in packaging table, so now all i have to do is to specify variable getting the value of selected column, do not know how to do that .. tried many options, some of them not logical too ..

  7. #7
    Join Date
    Aug 2012
    Posts
    155
    Can you provide the structure of your packaging table as the problem you are having makes no sense at all.

  8. #8
    Join Date
    Jun 2007
    Posts
    25
    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";
    }

  9. #9
    Join Date
    Aug 2012
    Posts
    155
    Yes, I understand what you want to achieve but I can't work out why it isn't working for you. Are you now saying that when you hard code the query (as above) or run directly in phpmyadmin it works as expected, it is just when you run it within the other while loop that it doesn't work?

  10. #10
    Join Date
    Jun 2007
    Posts
    25
    packaging _items table:

    id
    name
    cat
    varname - each record (ther is 14 of then at the moment ) haas name pack01, pack 02 ...so on
    info
    price
    photo

    packaging table:

    id
    datestamp
    ----
    pack01
    pack02
    pack03
    pack04
    pack05
    pack06
    pack07

    ---- same amount 14 at the moment

  11. #11
    Join Date
    Jun 2007
    Posts
    25
    $result1=mysql_query("SELECT pack01 FROM packaging WHERE orderno='$orderno' LIMIT 1") or die(mysql_error());
    while($row1 = mysql_fetch_array( $result1 )) {

    ////// must be here , but i have no idea how to rewrite this variable dynamicly ...

    ////// $pack01 = $row1['pack01'];

    echo "$pack01";
    }

  12. #12
    Join Date
    Aug 2012
    Posts
    155
    Where is your orderno column in your packaging table, you have no listed it as a column name?

  13. #13
    Join Date
    Jun 2007
    Posts
    25
    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>"; }

    }

    special thx to simplypixie

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles