私はMysqlの簡単なデータ検索PHPファイルを持ち、JSON文字列でエンコードします。以下のコードは期待PHPで関数が結果を返さない
<?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();
?>
サンプル結果として結果を返す...
{"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();
?>
あなたはphp.netのドキュメントにhttp://php.net/manual/fr/function.call-user-func.php
を見ることができるように機能のように文字列を受け入れる...
Error description: {"result":null}
これはうまくいきました。グローバル$ tablename; 'が機能します。 '$ result = $ tablename();' – archiver
@archiverあなたを助けてくれる:) :) –