我想通过array
实现以下代码:
if ($z=='one'){$result = mysql_query( " SELECT * FROM table1", $link);}
if ($z=='two'){$result = mysql_query( " SELECT * FROM table2", $link);}
我的尝试在这里:(完整代码)
$z = $_GET["z"];
$link = mysql_connect("localhost","root","");
mysql_select_db("DBname",$link);
function one(){
$result = mysql_query( " SELECT * FROM table1");
return $result;
}
function two(){
$result = mysql_query( " SELECT * FROM table2");
return $result;
}
$arr=array ('one'=>"one",'two'=>"two");
$result=$arr[$z]();
while($end = mysql_fetch_assoc($result)) {
$end["col1"];
$end["col2"];
$end["col3"];
}
mysql_close($link);
为什么这段代码不起作用?
Tnx朋友
答案 0 :(得分:0)
Description:
Stopped working
Problem signature:
Problem Event Name: APPCRASH
Application Name: bds.exe
Application Version: 21.0.17707.5020
Application Timestamp: 545bd62d
Fault Module Name: rtl210.bpl
Fault Module Version: 21.0.17707.5020
Fault Module Timestamp: 545bd940
Exception Code: c0000005
Exception Offset: 00016a9c
OS Version: 6.1.7601.2.1.0.768.3
Locale ID: 1033
答案 1 :(得分:0)
问题是$arr
的内容不是您使用相同名称定义的功能(为什么会这样?)。看起来您正在尝试使用closure
$arr['one'] = function() {
$result = mysql_query( " SELECT * FROM table1");
return $result;
};
$arr['two'] = function() {
$result = mysql_query( " SELECT * FROM table2");
return $result;
};
然后调用那些你可以使用现有代码而不需要if(甚至更快然后检查值):
$result = $arr[$z];
您也可以使用call_user_func_array()
,并保留现有的功能定义。这看起来就像你最初想要实现的那样。
$arr = array ('one'=>"one",'two'=>"two");
if (isset($arr[$z])) $result = call_user_func_array($z,array());