Copyright © 2002-2022 Judd Vinet, Aaron Griffin and Levente Polyák. Show
The Arch Linux name and logo are recognized trademarks. Some rights reserved. The registered trademark Linux® is used pursuant to a sublicense from LMI, the exclusive licensee of Linus Torvalds, owner of the mark on a world-wide basis. I have a Problem: Table of Contents
I set up a local LAMP WebServer on a Raspberry Pi. On there (http://ipaddress/inpData) one can Input data which then gets stored in the MYSQL database (via php). On that same Server I have a JSON string (http://ipaddress/file.json) which I access from another Raspberry pi in that Network (everything is connected through a router) which gives the Input for some functions on that Pi. Now my Problem: I want to alter the JSON string depending on the data the user on Pi1 puts in (which gets stored in the MYSQL db). I managed to Access and also filter the data from the database (using php and MYSQL Statements in a seperate file on the Server). However how do I "transfer" them to the JSON string that I use? I Need it to be dynamic so I planned on using php but after googling for hours and trying 78234523645 methods that didn't work I am still clueless. It seems so simple and yet I can't figure it out. Your help is greatly appreciated, thankyou :-) Hannah asked Aug 19, 2016 at 13:28 1 There are 2 functions in PHP you would need to do what you need
json_encode takes a value or array and converts it to json format file_put_contents allows you to write the json or any text to a file it creates the file if it does not exist and overwrites if it does. you could do this
or
either way file.json gets populated with data from the database. see php.net for details on the functions. answered Aug 19, 2016 at 13:50 keefokeefo 263 bronze badges Not the answer you're looking for? Browse other questions tagged php mysql json server lamp or ask your own question.Community Blog How to Return MySQL Data in JSON Format With PHP On Ubuntu 20.04 Server How to Return MySQL Data in JSON Format With PHP On Ubuntu 20.04 ServerIn this guide, you'll set up a simple MySQL database and then use some PHP functions to retrieve and convert data in JSON format. IntroductionJavaScript Object Notation(JSON) has become the standard data interchange format when working with Application Programming Interfaces(APIs). JSON has a simple syntax that is easily readable by both humans and machines compared to legacy formats like Extensible Markup Language(XML). In addition, most modern programming languages provide tonnes of libraries for encoding and parsing JSON making it the preferred format for many developers. In this guide, you'll set up a simple MySQL database and then use some PHP functions to retrieve and convert data in JSON format. You'll test this guide on an Ubuntu 20.04 server. PrerequisitesBefore you begin, make sure you've the following:
Step 1 - Creating a Sample Database and a TableIn this step, you'll set up a sample database and a table. You'll also populate the table with some records that you'll use throughout this guide to test the JSON format. Begin by connecting to your server and issue the command below to log in to your MySQL as root.
Enter your root password for your MySQL server and press ENTER to proceed. Then, issue the following command to create a
Confirm the output below to make sure you've created the database:
To access the new
You should receive the following output confirming that you've successfully created the user:
Next, issue the
You should receive the following output to confirm you've switched to the new database without errors:
Next, set up a sample
Confirm the following output to ensure you've created the table without any errors:
Next, use the
You should get the following output to confirm the
Make sure the data is in place by running a
You should get the following output listing all records in the
Exit from MySQL command-line interface:
Output:
You now have the database, tables, and some sample records. In the next step, you'll code a PHP script that fetches records from the MySQL database and converts the output to JSON format. Step 2 - Creating a PHP ScriptIn this step, you'll use the PHP Data Object(PDO) library to connect to the database that you've created in Step 1 and retrieve records from the
Then, begin by opening a new
Next, initialize an SQL statement with the correct syntax for fetching data from the
When you've finished editing, your
Save and close the file by pressing CTRL + X, Y, and ENTER. You now have the correct PHP code for retrieving data from the MySQL database and encoding it to JSON. In the next step, you'll run the code and see what happens. Step 3 - Testing the PHP ScriptIn this step, you'll execute your PHP file to test the functionality of the code you've written. Open a new browser window and enter the URL below. Replace
You should now receive the following JSON response on your browser:
The output above confirms that your PHP code is working and indeed, you've successfully converted your database records to the standard JSON format. ConclusionIn this tutorial, you've set up a sample database, created a table, and populated it with some records. Then, you've used the PDO library to fetch data from your database and finally used some PHP functions to convert the data to the JSON format. Use this guide when working on your next API application. francisndungu31 posts | 8 followers FollowYou may also likeCommentsfrancisndungu31 posts | 8 followers Follow
|