我正在传递JSON输入,然后解码值,然后将值传递给SQL查询,以根据我提到的条件从表中获取数据。但对我来说,当我在网页中打开PHP脚本时,它以JSON格式显示表的所有值。此外,我没有从我的输入中获取DB的值。之前这个脚本正在运行。这个脚本有什么问题。
$jsonString ='{"imdblist":{},"rottenlist":{},"generelist":{"genere1":"Documentary"},"yearlist":{}}';
$obj = json_decode($jsonString,true);
$imdb=array();
$rotten=array();
$genere=array();
$year=array();
foreach ($obj['imdblist'] as $key => $value)
{
echo "<br>------" . $key . " => " . $value;
$imdb[$key] = $value;
}
foreach ($obj['rottenlist'] as $key => $value)
{
// echo "<br>------" . $key . " => " . $value;
$rotten[$key] = $value;
}
foreach ($obj['yearlist'] as $key => $value)
{
//echo "<br>------" . $key . " => " . $value;
$year[$key] = $value;
}
$val1=implode("','", $imdb);
$val1 = "'" . $val1 . "'";
$val2=implode("','", $rotten);
$val2 = "'" . $val2 . "'";
$val4=implode("','", $year);
$val4 = "'" . $val4 . "'";
array_walk($obj['generelist'],function(&$item1, $key){$item1="$item1=1";});
$stringgen = implode(' OR ', $obj['generelist']);
if(empty($stringgen)){
$myquery = "SELECT * FROM Moviess WHERE `imdb_rat` IN ($val1) OR `Rotten_rate` IN ($val2) OR `Year` IN ($val4)";
}
else {
$myquery = "SELECT * FROM Moviess WHERE `imdb_rat` IN ($val1) OR `Rotten_rate` IN ($val2) OR ".mysql_real_escape_string($stringgen)." OR `Year` IN ($val4)";
}
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
在上面的查询中,我只需要获取Documentary的内容,但是当我运行查询时不会获取数据。如果我在网页中打开它,我还能够以JSON格式查看表中的所有值。这段代码有什么问题?