PHP MYSQL $row[$variable]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11752333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:59:59  来源:igfitidea点击:

PHP MYSQL $row[$variable]

phpmysqldatabaserowfetch

提问by Chinmay

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.

但是,我无法取回任何数据。我检查了打印查询并在 php my admin 中运行它,并且它按我的意愿工作。但我猜数组中的变量可能不起作用。请帮助我。谢谢。

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];

                    }
                }

回答by Fluffeh

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.

编辑:看了一点,这可能不正确。您可以设置 fetch_array 以返回关联数组。

You cannot parse variables through single quotes '

您不能通过单引号 ' 解析变量

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

or

或者

echo $row[$col_name];

or

或者

echo $row["$col_name"];

回答by Jonathan de M.

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

因为就像@Fluffeh 所说的,它不是关联数组

回答by Brayden

Have you tried looping through the results array as such:

您是否尝试过循环遍历结果数组:

while($row = mysql_fetch_array($result)) {
echo $row['col_name'];
echo "<br />";
}

回答by Conrad Lotz

I would suggest the following solution. Give the parameter $col_nameand AS Col_NameThen the $row['Col_Name'] can always be set to the parameter no matter what the value.

我会建议以下解决方案。设置参数$col_nameAS Col_Name随后$行[“COL_NAME”]总是可以被设置为参数无论什么价值。

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