Solved

How I encode JSON a query sql with php?

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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 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 …

687 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