Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-29
3
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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 111

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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 dynamically set the form action using jQuery.
Suggested Courses

610 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