Php JSON返回所有空值
我试图从MySQL数据库中获取JSON,但它无法恢复任何内容。这是我的代码: db_config.phpPhp JSON返回所有空值,php,mysql,json,Php,Mysql,Json,我试图从MySQL数据库中获取JSON,但它无法恢复任何内容。这是我的代码: db_config.php <?php /* * All database connection variables */ define('DB_USER', "root"); // db user define('DB_PASSWORD', ""); // db password (mention your db password here) define('DB_DATABASE', "biblioa
<?php
/*
* All database connection variables
*/
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "biblioapp"); // database name
define('DB_SERVER', "localhost"); // db server
?>
获取_all_products.php
<?php
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM registres") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["registres"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $result["id"];
$product["titol"] = $result["titol"];
$product["autor"] = $result["autor"];
$product["descripcion"] = $result["descripcion"];
// push single product into final response array
array_push($response["registres"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
如果我转到localhost/json/get_all_products.php,我会收到以下消息:
{"success":0,"message":"Required field(s) is missing"}
{"registres":[{"id":null,"titol":null,"autor":null,"descripcion":null},{"id":null,"titol":null,"autor":null,"descripcion":null},{"id":null,"titol":null,"autor":null,"descripcion":null},{"id":null,"titol":null,"autor":null,"descripcion":null},{"id":null,"titol":null,"autor":null,"descripcion":null}],"success":1}
这里怎么了?谢谢
while ($row = mysql_fetch_array($result)) {
$product = array();
$product["id"] = $result["id"];
您的结果不在$result
中,而是在$row
中:
$product["id"] = $row["id"];
在您的问题中,我看不到json有什么共同之处:-) 唯一的问题是,您创建的数组中的值为空 您没有向我们提供有关您所拥有的表结构以及您试图调试的值的任何信息 因此,启动更好调试的快速修复方法可以是:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$response["registres"][] = $row;
}
但是在查看您的代码时,我想建议您停止使用不推荐的mysql_uu调用。使用mysqli或PDO。这样我会收到id,但其他营地是空的。这是我第一次使用JSON,感谢您的支持help@Santiago嗯,您还必须对其他变量使用
$row
:$row['titol',$row][…
我以前尝试过,但我收到了以下消息:注意:未定义索引:E:\xampp\htdocs\json\get_all_products.php中的description,在第31行,没有description变量,只显示一个白色页面。因此$\u POST['search']
未设置。这看起来像是一种非常奇怪的复制。我只是想在代码方面寻求一些帮助,我一直在尝试以不同的方式创建JSON文件,这是最接近获得结果的,这似乎是一个简单的过程,但我总是有错误或系统没有响应,所以如果我忽略了什么,我会询问review,n其他。如我所说,get\u products\u details.php
的问题是我在第一篇评论中提到的,它与数据库或JSON无关。你能告诉我更多的解释我应该做什么吗?我对所有这些都不熟悉,非常感谢你的时间。
while ($row = mysql_fetch_array($result)) {
$product = array();
$product["id"] = $result["id"];
$product["id"] = $row["id"];
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$response["registres"][] = $row;
}