Solved

How I encode JSON a query sql with php?

Posted on 2008-10-26
4
2,129 Views
Last Modified: 2013-12-13
How I econde a result of a Mysql query as JSON data? with php

This is the format of JSON data:
[{"1":"Data 1"},{"2":"Data 2"},{"3":"Data 3"}]

Open in new window

0
Comment
Question by:denny3d
4 Comments
 
LVL 15

Accepted Solution

by:
MMDeveloper earned 250 total points
ID: 22807260
if you have a newer version of PHP, you can do something like this (untested)

$results = mysql_query($yourQuery);
$jsonData = array();

while ($row = mysql_fetch_assoc($result)) {
      $jsonData[] = json_encode($row);
}

$jsonData = "{" . implode(", ", $jsonData) . "}";


you could wrap that in a reusable function to make it look cleaner.
0
 
LVL 7

Expert Comment

by:ljubiccica
ID: 22807849
You get the data and want to use it with php?
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

17 Experts available now in Live!

Get 1:1 Help Now