我有一个来自Mysql的简单数据检索PHP文件,并以JSON字符串编码。下面的代码按预期返回结果
<?php
require 'dbconnection.php';
$tablename = $_GET["tabname"];
$sql = "SELECT * FROM ". $tablename ;
if (!mysqli_query($conn,$sql))
{
echo("Error description: " . mysqli_error($con));
} else {
$res = mysqli_query($conn,$sql);
}
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('_id'=>$row[0],
'course_name'=>$row[1],
'address'=>$row[2],
'city'=>$row[3],
'state'=>$row[4],
'zipcode'=>$row[5],
'phone'=>$row[6]));
}
echo json_encode(array("result"=>$result));
$conn->close();
&GT?;
示例结果......
{"result":[{"_id":"1","course_name":"Quail Valley","address":"12565 NW Aerts Rd.","city":"Banks","state":"OR","zipcode":"97106","phone":"5033244444"},...]}
我的目标是使用传递给PHP的变量并基于它的名称调用函数。我似乎无法弄清楚我做错了什么!
<?php
require 'dbconnection.php';
$tablename = $_GET["tabname"];
function Course() {
$sql = "SELECT * FROM ". $tablename ;
if (!mysqli_query($conn,$sql))
{
echo("Error description: " . mysqli_error($conn));
} else {
$res = mysqli_query($conn,$sql);
}
$results = array();
while($row = mysqli_fetch_array($res)){
array_push($results,
array('_id'=>$row[0],
'course_name'=>$row[1],
'address'=>$row[2],
'city'=>$row[3],
'state'=>$row[4],
'zipcode'=>$row[5],
'phone'=>$row[6]));
}
return $results;
}
$result = call_user_func(Course());
// OR... $result = call_user_func($tablename());
echo json_encode(array("result"=>$result));
$conn->close();
&GT?;
这是输出......
Error description: {"result":null}
答案 0 :(得分:1)
正如您在php.net http://php.net/manual/fr/function.call-user-func.php
的文档中看到的那样该函数接受一个字符串作为参数(参见示例)。
所以它应该是call_user_func('Course')
答案 1 :(得分:0)
首选方式是: -
$result = Course();
但 如果您想使用call_user_func()
,请按以下方式使用: -
$result = call_user_func('Course');
这是因为它将回调作为字符串: - call_user_func