1

I have a Problem:

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

Hannah
  • 11
  • 1
  • So you just want to create JSON from MYSQL results? Like this? http://stackoverflow.com/questions/383631/json-encode-mysql-results – Fels Aug 19 '16 at 13:42

1 Answers1

0

There are 2 functions in PHP you would need to do what you need

  • json_encode()
  • file_put_contents()

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

$json = json_encode($mysql_result);
file_put_contents("file.json", $json);

or

file_put_contents("file.json", json_encode($mysql_result));

either way file.json gets populated with data from the database. see php.net for details on the functions.

keefo
  • 26
  • 3