PHP MYSQL $row[$variable]

54,427

Solution 1

You are fetching an array, not an assoc array. Use this:

echo "<br>".$row[0];

Edit: Having looked a little more, this may not be correct. You can set fetch_array to return assoc arrays.

You cannot parse variables through single quotes '

echo $row['col_name'];  // manually typed string.

or

echo $row[$col_name];

or

echo $row["$col_name"];

Solution 2

You tried that->

echo "<br>".$row[$col_name];

OR

    $myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
    $result = mysql_query($myQuery);
    $row = mysql_fetch_assoc($result);
    echo "<br>".$row[$col_name];

Cause like said @Fluffeh it's not a associative array

Share:
54,427
Chinmay
Author by

Chinmay

Updated on July 25, 2022

Comments

  • Chinmay
    Chinmay almost 2 years

    I am trying to work around with dynamic table creation and data fetching. I am trying to get the data using following code :

        $myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
        $result = mysql_query($myQuery);
        $row = mysql_fetch_array($result);
        echo "<br>".$row['$col_name'];
    

    But, I am unable to get any data back. I checked printing the query and running it in php my admin and its working as I want. But I guess variable in array might not be working I guess. Please help me regarding the same. Thanks.

    The Whole loop looks something like this :

    $myQuery = "SELECT * FROM information_schema.columns WHERE table_name = '$tabname'";
    $re = mysql_query($myQuery);
    
    while($row = mysql_fetch_array ($re)){
             if(!empty ($row)){
                        $col_name = $row['COLUMN_NAME'];
    
              $myQuery = "SELECT ".$col_name." FROM ".$tabname." WHERE sampleid='".$sid."'";
                        echo "<br>".$myQuery;
                        $reqq = mysql_query($myQuery);
                        $roww = mysql_fetch_array($reqq);
                        echo "<br>".$roww[$col_name];
    
                        }
                    }