JSON返回所有NULL

时间:2015-03-19 16:12:54

标签: 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', "biblioapp"); // database name
define('DB_SERVER', "localhost"); // db server
?>

db_connect

<?php

/**
 * A class file to connect to database
 */
class DB_CONNECT{

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

        // Selecing database
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    }

}

?>

get_products_details.php

<?php

$ response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
$search = '';
    if (isset($_POST['search'])){
        $search = strtolower($_POST['search']);

    // get a product from products table
    $result = mysql_query("SELECT * FROM registres WHERE titol LIKE '%".$search."%' OR autor LIKE '%".$search."%'");


    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["id"] = $result["id"];
            $product["titol"] = $result["titol"];
            $product["autor"] = $result["autor"];
            $product["descripcion"] = $result["descripcion"];

            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

get_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_product_details.php,我会收到:

{"success":0,"message":"Required field(s) is missing"}

如果我去localhost / json / get_all_products.php,我会收到:

{"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}

这里有什么问题?感谢...

2 个答案:

答案 0 :(得分:0)

while ($row = mysql_fetch_array($result)) {
    $product = array();
    $product["id"] = $result["id"];

您的搜索结果不在$result,而在于$row

$product["id"] = $row["id"];

答案 1 :(得分:0)

我发现你的问题与json没什么共同之处: - )

您遇到的唯一问题 - 您创建的数组中的空值。

您没有向我们提供有关您拥有的表结构以及您尝试调试的值的任何信息。

因此,只需快速修复即可开始更好的调试:

 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $response["registres"][] = $row;
 }

但是检查你的代码我想建议你停止使用已弃用的mysql_调用。使用mysqli或PDO。