Solved

How I encode JSON a query sql with php?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

15 Experts available now in Live!

Get 1:1 Help Now