PHP MYSQL $ row [$ variable] [英] PHP MYSQL $row[$variable]

查看:92
本文介绍了PHP MYSQL $ row [$ variable]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理动态表创建和数据获取.我正在尝试使用以下代码获取数据:

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

但是,我无法获取任何数据.我检查了打印查询并在php我的管理员中运行它并根据需要运行它.但是我想数组中的变量可能无法正常工作. 请同样帮助我.谢谢.

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

                    }
                }

推荐答案

您正在获取数组,而不是

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

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

看起来多一点,这可能是不正确的.您可以设置fetch_array返回assoc数组.

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

您不能通过单引号'

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

echo $row[$col_name];

echo $row["$col_name"];

这篇关于PHP MYSQL $ row [$ variable]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆