2

I have written a php code to make JSON file from mysql database. When I checked the JSON file, it displays some data NULL, though my database has no NULL data.. Here is my PHP CODE:

$selectQuery = "SELECT tour_code, day, schedule_title, details FROM tbl_tour_details WHERE status=0";
    $response = array();
    $posts = array();
    $result = mysql_query($selectQuery) or die ("Errored Query: ".$selectQuery);
    if(mysql_num_rows($result))
    {
        while($row = mysql_fetch_assoc($result))
        {
            $tour_code = $row['tour_code'];
            $day = $row['day'];
            $schedule_title = $row['schedule_title'];
            $details = $row['details'];

            $posts[] = array('tour_code'=>$tour_code, 'day'=>$day, 'schedule_title'=>$schedule_title, 'details'=>$details);

        }
    }
    $response['posts'] = $posts;
    $fp = fopen('..//webservices//json//tour_details.json','w');
    fwrite($fp, json_encode($response));
    fclose($fp);

Here, DATATYPE and Size is:

tour_code = int
day = int
schedule_title = varchar(150)
details = text

My data is look like this:

tour_code =1
day = 3
schedule_title = "Singapore – Genting Highland By Coach (350 KM 5 HRS)"
details = "This Morning Is The Day Of FUN And Adventure For The Family. Enjoy Spectular Rides And Shows At The Ever-Popular Genting Outdoor Theme Park With Its Myriad Attractions For The Young And Old Alike. Take An Exciting Drive In The Antique Car Or Daring Spins On The Roller Coaster, It’s A Magical Adventure Of Fun And Excitement For The Whole Family!
Over Night At Hotel First World Deluxe Or Theme Park Or Awana Resort Or Similar.
(Breakfast, Lunch & Dinner)"

Here is my json file(Which consist if null, /r, /n): json file

Here is my var_dump file(which truncate some data as "...") var_dump file

Here is my print_r($posts) file(which is totally correct): print_r($posts file)

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Dhwani
  • 7,484
  • 17
  • 78
  • 139
  • what is value of print_r($posts); after loop.. – Pankaj Khairnar Jan 02 '13 at 07:58
  • Just take a look in the manual, the return value of the [`json_decode` function](http://php.net/json_decode), including the error cases and how you can get more information about any potential error, is explained there: http://php.net/json_decode – hakre Jan 02 '13 at 15:02
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/a/14110189/1723893). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – NullPoiиteя Jan 02 '13 at 15:07
  • Thanks. It's my project and due to limitation of time and I dnt know about latest concept so I have to use this. But I will learn new concepts. Thanks for suggestion. – Dhwani Jan 02 '13 at 15:21

3 Answers3

3

you have corrupted utf-8 data

quick fix:

$details = iconv("UTF-8", "UTF-8//IGNORE", $row['details']);
skyline26
  • 1,994
  • 4
  • 23
  • 35
1

Please print the array in the browser and view the source of the page by pressing ctrl+u It will print the whole array with values in proper format and see if there are any null values in it...

while($row = mysql_fetch_assoc($result))
        {
            print_r($row);
        }

Hope the above code helps you to atleast found out where the problem is.....

vascowhite
  • 18,120
  • 9
  • 61
  • 77
THE ONLY ONE
  • 2,110
  • 1
  • 12
  • 6
0

First try doing a var_dump($response); just before the fopen to confirm that $response is actually getting the correct data.

Second your file name does not look correct to me. What's with the double forward slashes (/)? You only need to escape backslashes (\) because backslash is an escape character.

Change this line:

$fp = fopen('..//webservices//json//tour_details.json','w');

to this:

$fp = fopen('../webservices/json/tour_details.json', 'w');

Chances are that this might work for you but it doesn't just put a comment and we can look into it further.

Tanzeel Kazi
  • 3,797
  • 1
  • 17
  • 22
  • I checked var_dump, It is showing all data but in some of data, it truncate like "Meal & D...". I mean it put '...' after some of data – Dhwani Jan 02 '13 at 07:29
  • @Dhwani Did you do the changes to `fopen`? Also could you post your json file and explain what exactly you are getting `null`? – Tanzeel Kazi Jan 02 '13 at 07:41
  • Ya I have changed with json. Ok I will put json file here. – Dhwani Jan 02 '13 at 07:45