调用 PHP 数组中的静态变量


calling a static variable inside php array

我正在尝试使用 php 静态变量获取数组值,如下所示

$a = $_GET['type'];
if($a==1)
{
 $variable  = 'cost';
 $tablename = 'logistics_bluedart';
}
else if($a==2)
{
  $variable = 'shp_cost';
  $tablename = 'logistics_prof';
}
else if($a==3)
{
  $variable ='shipping_cost';
  $tablename = 'logistics_firstflight';
}
$myQuery = mysql_query("select $variable from $tablename");
while($resultData = mysql_fetch_array($myQuery)){
echo $resultData[$variable];
}

但是我没有得到任何输出?

更改此行:

$myQuery = mysql_query("select $variable from logistics where $type = $a");

取代:

$myQuery = mysql_query("select $variable from logistics where type = $a");//replace $type to type

试试这个:

check the $_GET['type'] must not empty.
then check $a must have the values in 1,2,3.

如果未遵循上述任何一项,则根据上面提到的代码,您将无法获得结果。

-

谢谢

请检查 while 循环

print_r($resultData);

它是返回数组,你有 Kay 喜欢成本,shp_cost,shipping_cost 该键在数组中。

Other wise used mysql_fetch_assoc($myQuery) instend of mysql_fetch_array($myQuery).