2
<?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 Validator Shows

{
    "news": [
        {
            "Id": null,
            "Menu": null,
            "SubPage": null,
            "PageName": null,
            "NewsTitle": null,
            "PageContent": null,
            "Image": null,
            "MenuType": null,
            "Date": null
        }
    ]
}

I am using this for fetching Bengali news from mysql database and parsing through json script for android. Can you please guide me why i am getting null where my db credential, parameters are completely ok.

Any kind help is appreciated,

Thank in advance Ishtiaque

4
  • Please post what issue are you facing and Android code also Commented Jul 6, 2015 at 8:32
  • Android Code is ok, when i m parsing this JSON url it supposed to give me some output of news stored in db. But it is returning me Null though there is couple of news in Database. For this Android app does not display news in the app Commented Jul 6, 2015 at 8:34
  • You are resetting $response["news"] = array(); instead of product :) Commented Jul 6, 2015 at 8:35
  • That i did to see whether it works or not and this is what i got in browser parsing. {"news":[{"Id":null,"Menu":null,"SubPage":null,"PageName":null,"NewsTitle":null,"PageContent":null,"Image":null,"MenuType":null,"Date":null}]} @Abhilash Cherukat Commented Jul 6, 2015 at 8:37

2 Answers 2

3

You are using the wrong variable for your result set. Change this

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;
}

In fact you could reduce this code quite a bit by doing

$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);
}

You should also consider using mysqli_ or pdo mysql database extension rather than mysql_ as the mysql_ extension is deprecated and in PHP7 will be gone forever.

Sign up to request clarification or add additional context in comments.

2 Comments

That my friend requires that you ask another question specifically related to this new problem.
Sorry i m removing the another question related to this. Can you please check the URL above
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);
}
?>

You were trying to access the wrong array, you were using $result instead of $row when you're defining $product array.

4 Comments

Here i should replace these with ? $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"];
@AMIshtiaqueSarwar What ? If you look at the code i've inserted and the code you have, the difference is in the while loop, you're using $result[] to set $product[], but you have to use $row[], since that's the variable you're binding mysql_fetch_array to.
Are you talking about this? $response["news"] = array(); while ($result = mysql_fetch_array($row)) { // temp user array $product = array(); $product["Id"] = $row["Id"]; $product["Menu"] = $row["Menu"];
Yes, you can see i've replaced result with row, to pull out the right values as you want. You're trying to access values that doesn't exist since $result isn't an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.