Solved

PHP script to query MYSQL database and return results in JSON format

Posted on 2011-09-29
3
489 Views
Last Modified: 2012-06-22
Hi, I have a PHP/MYSQL calendar script that updates availability for hotels based on their respective room types. Now I need to query that database to return availability based on a given date range and room type, and the result has to be returned in JSON format. Any help will be greatly appreciated.
0
Comment
Question by:Jorge Batres
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
loop over you records/data from your mysql database, don't echo anything else (no html,, head, body tag) in your script
create an array of hash table for each record
and use : http://php.net/manual/en/function.json-encode.php
to transform the array

$days = array();

while ($row = mysql_fetch_assoc($result)) {
    $days[] = array( "field0"=>$row["field0"], "field1"=>$row["field1"], "field2"=>$row["field2"] );
}

header('Content-Type: application/json');
echo json_encode($days);

?>

Open in new window


0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
No points for this, leakim971 gave the first good answer.

I think you could simplify that code structure and thereby simplify the JSON string.  I might try something like this for the while() loop.
while ($row = mysql_fetch_assoc($result)) {
    $days[] = $row;
}

Open in new window

0
 

Author Comment

by:Jorge Batres
Comment Utility
Thank you both!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now