Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried to use Json to send data but it failed. When I use the jsonviewer, I find it only read part of my structure(I have 'pid','name', 'price' , 'description', 'uses', 'sideeffects', 'precauctions', 'interactions', 'overdose, it can only read 'name', 'price' , 'description'):

Below is the php code:
<?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();

// check for post data
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM products WHERE pid = $pid");

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

            $result = mysql_fetch_array($result);

            $product = array();
            $product["pid"] = $result["pid"];
            $product["name"] = $result["name"];
            $product["price"] = $result["price"];
            $product["description"] = $result["description"];
            $product["uses"] = $result["uses"];
            $product["sideeffects"] = $result["sideeffects"];
            $product["precauctions"] = $result["precauctions"];
            $product["interactions"] = $result["interactions"];
            $product["overdose"] = $result["overdose"];
            $product["created_at"] = $result["created_at"];
            $product["updated_at"] = $result["updated_at"];
            // 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);
}
?>
Posted

1 solution

Did you notice that you lost the $result object initially retrieved from the database? In this line:
PHP
$result = mysql_fetch_array($result);

What would you expect if you are not using all the data set. Please see the documentation:
http://php.net/manual/en/function.mysql-fetch-array.php[^].

Pay attention that it "Returns an array that corresponds to the fetched row and moves the internal data pointer ahead". That said, if you can this function again, you will get another row, so you can do it in cycle until you get FALSE in return (please see "Return Values"). But you cannot do it because you lost $result object returned from the call to mysql_query. Just introduce another variable:
PHP
$row = mysql_fetch_array($result);


You may have other bugs, this is just the first apparent one.

—SA
 
Share this answer
 
Comments
Member 11437984 1-Mar-15 22:56pm    
Yes, I think I really have another bug. After I add the code $row = mysql_fetch_array($result); , the bug is still here.
Sergey Alexandrovich Kryukov 1-Mar-15 23:02pm    
I don't know what have you really done. After you calculate $row, you need to properly use this object, not just assign new object to this $row variable...
Isn't it logical?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900