<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get a product from products table
$result = mysql_query("SELECT * FROM news WHERE Menu = 'FirstPage'");
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["news"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["Id"] = $result["Id"];
$product["Menu"] = $result["Menu"];
$product["SubPage"] = $result["SubPage"];
$product["PageName"] = $result["PageName"];
$product["NewsTitle"] = $result["NewsTitle"];
$product["PageContent"] = $result["PageContent"];
$product["Image"] = $result["Image"];
$product["MenuType"] = $result["MenuType"];
$product["Date"] = $result["Date"];
// success
// $response["success"] = 1;
// user node
$response["news"] = array();
array_push($response["news"], $product);
}
// success
// $response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
?>
JSON验证器显示
{
"news": [
{
"Id": null,
"Menu": null,
"SubPage": null,
"PageName": null,
"NewsTitle": null,
"PageContent": null,
"Image": null,
"MenuType": null,
"Date": null
}
]
}
我正在使用它来从mysql数据库获取孟加拉新闻并通过json脚本解析android。你能否指导我为什么我的db凭证,参数完全没问题。
感谢任何帮助,
提前感谢 Ishtiaque
答案 0 :(得分:3)
您在结果集中使用了错误的变量。改变这个
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["Id"] = $row ["Id"];
$product["Menu"] = $row ["Menu"];
$product["SubPage"] = $row ["SubPage"];
$product["PageName"] = $row ["PageName"];
$product["NewsTitle"] = $row ["NewsTitle"];
$product["PageContent"] = $row ["PageContent"];
$product["Image"] = $row ["Image"];
$product["MenuType"] = $row ["MenuType"];
$product["Date"] = $row ["Date"];
// Also you were resetting the $response['news']
// array here unnecessarily
$response["news"][] = $product;
}
实际上你可以通过
来减少这段代码$result = mysql_query("SELECT Id,Menu,SubPage,PageName,NewsTitle,
PageContent,Image,MenuType,Date
FROM news
WHERE Menu = 'FirstPage'");
if (mysql_num_rows($result) > 0) {
$response["news"] = array();
while ($row = mysql_fetch_array($result)) {
$response["news"][] = $row;
}
echo json_encode($response);
}
您还应该考虑使用mysqli_
或pdo
mysql数据库扩展而不是mysql_
,因为mysql_
扩展名已弃用,而PHP7将永远消失。
答案 1 :(得分:0)
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get a product from products table
$result = mysql_query("SELECT * FROM news WHERE Menu = 'FirstPage'");
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["news"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["Id"] = $row["Id"];
$product["Menu"] = $row["Menu"];
$product["SubPage"] = $row["SubPage"];
$product["PageName"] = $row["PageName"];
$product["NewsTitle"] = $row["NewsTitle"];
$product["PageContent"] = $row["PageContent"];
$product["Image"] = $row["Image"];
$product["MenuType"] = $row["MenuType"];
$product["Date"] = $row["Date"];
// success
// $response["success"] = 1;
// user node
$response["news"] = array();
array_push($response["news"], $product);
}
// success
// $response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
?>
你试图访问错误的数组,当你定义$ product数组时,你使用的是$ result而不是$ row。