【发布时间】:2015-04-03 12:00:20
【问题描述】:
我一直在使用 MySQL 开发这个 Android 应用程序 -> PHP 和 JSON 用于学校作业。我对 PHP 没有经验,我在这个 php 文件中找不到错误。
<?php
require("db_config.php");
//initial query
$query = "SELECT * FROM GAME G, CONSOLE C WHERE G.consoleid = C.consoleid";
//execute query
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rows = $stmt->fetchAll();
if ($rows)
{
$response["success"] = 1;
$response["message"] = "Games Available!";
$response["games"] = array();
foreach ($rows as $row)
{
$gameslist = array();
$games["gameid"] = $row["gameid"];
$games["gamename"] = $row["gamename"];
$games["gamevalue"] = $row["gamevalue"];
$games["gamerarity"] = $row["gamerarity"];
$games["gamedescription"] = $row["gamedescription"];
$games["consolename"] = $row["consolename"];
$games["gameimgstring"] = $row["gameimgstring"];
//update our repsonse JSON data
array_push($response["games"], $gameslist);
}
// echoing JSON response
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "No games Available!";
die(json_encode($response));
}
?>
PHP 脚本应该返回一个包含游戏数据的数组。 Query 不需要任何数据。
我的 IDE 在这部分出现错误
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query);
}
由于失败,它不会返回值。
【问题讨论】:
-
你不需要把sql语句放在
->execute()里面
标签: java php android mysql json