0

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)

    $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!

Nita
  • 561
  • 5
  • 28
  • store the result of first query before you use it in the next query. – Shiplu Mokaddim Jan 08 '13 at 06:30
  • what does mysql_error() say? ( `echo mysql_error();` right after `$result1 = ...` ) – hek2mgl Jan 08 '13 at 06:33
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Jan 08 '13 at 06:36
  • Yes mysql is deprecated, but declaring a XX years old api as depreacted, just to add an 'i' to functions isn't very clever too ;) SO should add a filter `str_replace('mysql', 'mysqli')...` (knowing that there are more enhancements than the 'i') – hek2mgl Jan 08 '13 at 06:38
  • @hek2mgl when you add prepared feature, it __is__ clever ;) – itachi Jan 08 '13 at 06:39

1 Answers1

0

I am not sure that this one will solve the problem, anyway try as follows,

$result = mysql_query("SELECT * FROM packaging_items") or die(mysql_error());  
while($row = mysql_fetch_assoc($result)) {  
    $data1 = $row['varname']; 
    $name = $row['name']; 
    $price = $row['price']; 
$query = "SELECT $data1 FROM packaging WHERE orderno='$orderno' LIMIT 1";
echo $query;
$result1=mysql_query($query) or die(mysql_error()); 
while($row1 = mysql_fetch_assoc( $result1 )) {

    if ( $data1 == '' ) {} else { 
    echo" <tr><td>$name</td><td>$data1</td><td>&pound;$price</td><tr>"; } 
} 
} 

It seems to the variable $row1 is not used anywhere.

HILARUDEEN S ALLAUDEEN
  • 1,722
  • 1
  • 18
  • 33