我正在尝试使用循环设置变量$ upkeep_1,$ upkeep_2等。 我有这段代码:
$sql_result2 = mysql_query("SELECT * FROM houses_db", $db);
while ($rs2 = mysql_fetch_array($sql_result2)) {
$upkeep_{$rs2[type]} = (int) $rs2[upkeep];
}
echo $upkeep_3 . "<br>";
$ rs2 [type]是一个数字字段,从1开始,最多为8。
尽管$ rs [upkeep]为3000,但这段代码没有输出任何内容。我做错了什么?
答案 0 :(得分:4)
为什么不使用数组?
$sql_result2 = mysql_query("SELECT * FROM houses_db", $db);
while ($rs2 = mysql_fetch_array($sql_result2)) {
$upkeep[$rs2['type']] = (int) $rs2['upkeep'];
}
echo $upkeep[3] . "<br>";
答案 1 :(得分:0)
你确定要这么做吗?你知道数组:)?
无论如何尝试这样的事情:
$sql_result2 = mysql_query("SELECT * FROM houses_db", $db);
while ($rs2 = mysql_fetch_array($sql_result2)) {
eval("$upkeep_".$rs2[type]." = (int) ".$rs2[upkeep]."");
}
echo $upkeep_3 . "<br>";