Solved

How I encode JSON a query sql with php?

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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.

734 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