Solved

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

Posted on 2011-09-29
3
493 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
ID: 36816115
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 109

Expert Comment

by:Ray Paseur
ID: 36891723
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
ID: 36893214
Thank you both!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Memory Keeps Increasing 4 35
Get Pixel color of the x and y axis Html without canvas 8 36
replace link with jQuery 4 14
Select case on click 3 16
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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