[英]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];
}
但是我沒有得到任何輸出?
請檢入while循環
print_r($resultData);
這是返回數組,您需要將key放入數組中的cost,shp_cost,shipping_cost等。
Other wise used mysql_fetch_assoc($myQuery) instend of mysql_fetch_array($myQuery).
更改此行:
$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.
如果以上任何一項都不遵循,那么您將無法根據上述代碼獲得結果。
--
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.