Solved

How I encode JSON a query sql with php?

Posted on 2008-10-26
4
2,149 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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.

821 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