How to generate JSON in realtime/dynamically from a MySQL database - php

So I want to export a JSON file from a MySQL database table, a php script that runs weekly and exports JSON file from a specific table.
This is sort of the thing I want to achieve:
<?php
$json_file_name = "File_export.json";
$json_file_name = str_replace(" ", "_", $json_file_name);
$con = mysqli_connect("", "", "", "");
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$date_range = array(
"start" => date("Y-m-d H:i:s", strtotime("-7 days")),
"end" => date("Y-m-d H:i:s", strtotime("now")),
);
and so on
if(!empty($json_data) && count($json_data) > 1)
{
$json_file_data = "";
$fp = fopen($json_file_name, 'w');
foreach($json_data as $row)
{
$json_file_data .= implode(",", $row) . "\n";
}
fwrite($fp, $json_file_data);
fclose($fp);
What is the best way to achieve the same.
Thank you :)

If your database table not too large, you can fetch all rows into a single array, then turn that array into JSON automatically without looping. This will generate JSON with column values as a list:
// $con is connection, $json_filename is name of filename to write
$query = "select * from MyTable";
// Fetch all rows into $json_data array
$result = mysqli_query($con, $query);
$json_data = mysqli_fetch_all($result);
mysqli_close($con);
// Turn data into JSON and write to file
$json = json_encode($json_data);
file_put_contents($json_filename, $json);
Example output:
[["name1","address1"],["name2","address2"]]
If your database table is a little bigger, it is better to write each line as it is generated. The code below will create a JSON object for each row.
$query = "select * from MyTable";
$result = mysqli_query($con, $query);
// Open output file
$fp = fopen($json_file_name, 'w');
// Write JSON list start
fwrite($fp, '[');
// Write each object as a row
$isFirstRow = true;
while ($row = mysqli_fetch_assoc($result)) {
if (!$isFirstRow) {
fwrite($fp, ',');
} else {
$isFirstRow = false;
}
fwrite($fp, json_encode($row));
}
// Write JSON list end
fwrite($fp, ']');
// Close file and MySQL connection
fclose($fp);
mysqli_close($con);
Example output:
[{"name": "name1", "address": "address1"},{"name": "name2", "address": "address2"}]

I think you also want to change this line:
$json_file_data .= implode(",", $row) . "\n";
to this:
$json_file_data[] = implode(",", $row);
Which will cause this:
$json = json_encode($json_data);
To deliver a json array of your database rows.

Related

Cannot write to my csv file from Oracle database using PHP

I think I used fputcsv correctly.My database table contains two columns with dates and numbers in both columns. I use an oracle database and a windows 2008 server.
//Connexion BDD Oracle
$connect = odbc_connect("oraclesrc", "xe", "sec123");
$query = "select * from timedata";
$result = odbc_exec($connect, $query);
$numOF = 0;
while(odbc_fetch_row($result)) {
$emp = odbc_result($result, 1);
$time = odbc_result($result, 2);
$numOF++;
echo $emp;
echo $time;
}
$fp = fopen('testfile.csv', 'w');
foreach ($row as $result) {
fputcsv($fp, $result);
}
fclose($fp);
odbc_close($connect);
The while loop fetches the result rows one at a time. If you change to use odbc_fetch_array($result) you will get an Assoc array exactly as fputcsv() want to see it.
//Connexion BDD Oracle
$connect = odbc_connect("oraclesrc", "xe", "sec123");
$query = "select * from timedata";
$result = odbc_exec($connect, $query);
$fp = fopen('testfile.csv', 'w');
while($row = odbc_fetch_array($result)) {
fputcsv($fp, $row);
}
fclose($fp);
odbc_close($connect);

CVS result shows in one column

I have this csv export code.
the export I working well but my only problem is that the result is showing only in one column
the problem happens only when I try to convert the file to UTF-8,
is there another way around this?
$link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB);
$link->set_charset("utf8");
$sql = "SELECT * FROM weekevents";
$query = mysqli_query($link,$sql);
$col = "col1,col2,col3,col4 \n";//Column headers
foreach ($query as $record){
$cam = explode(';', $record['cam']);
$col .= $record['weekday'].','.$record['week']
;
}
$filename = date("m-Y");
$csv_handler = fopen ('C:/xampp/htdocs/rge/main-rtl/uploads/שיבוץ חודש -'.$filename.'.csv','w');
fwrite ($csv_handler, $col);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';

Getting the right format of a .json file

I'm working on implementing a chart from highcharts. The data needed in order for it to be displayed needs to be in the .json format.
It has the following scheme:
[
[
1167609600000,
21
]
]
I'm getting my data from a mysql database via a php script:
<?php
//OPEN CONNECTION TO DB
$connection = mysqli_connect('localhost','abc','def','ghi') or die("Error " . mysqli_error($connection));
//SELECT ROWS NEEDED
$sql = "SELECT test, temp FROM database";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//CONVERT MYSQL TO PHP ARR
$json_array= array();
while ($row= mysqli_fetch_assoc($result))
{
$json_array[]= $row;
}
//WRITE DATA TO FILE
$fp = fopen('temps.json', 'w');
fwrite($fp, json_encode($json_array, JSON_PRETTY_PRINT));
fclose($fp);
//DISCONNECT FROM DB
mysqli_close($connection);
?>
Now comes my problem. The data generated by my script looks like this:
[
{
"a": "2",
"price": "15"
}
]
Firstly, I dont know how to supress the column-name of every result in my sql query. Secondly, I dont get why my script creates curly brackets instead of square ones. And thirdly, why does it use "." for every value.
Anybody able to help me out?
Thank you
You push in your array an associative array, so the key are kept and are displayed in your JSON:
while ($row = mysqli_fetch_assoc($result))
{
$json_array[] = [ (int)$row['a'], (int)$row['price'] ];
}
Will outputs:
[
[
2,
15
]
]
Another way is to use array_values():
while ($row = mysqli_fetch_assoc($result))
{
$vals = array_values($row);
$vals = array_map('intval', $vals);
$json_array[] = $vals;
}

exporting data in json format

I am trying to export data from PhpMyAdmin to JSON format
This code works for select lat,lng from googlemaps, but does not for the address column
My code is:
<?php
$con=mysqli_connect("localhost","root","","googlemaps");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT lat,lng,`addr` FROM infos";
if ($result = mysqli_query($con, $sql))
{
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row =mysqli_fetch_array($result))
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
$fp = fopen('results01.json', 'w');
fwrite($fp, "{ \"tab\": ");
fwrite($fp, json_encode($resultArray));
fwrite($fp, " }");
fclose($fp);
echo json_encode($resultArray);
//var_dump($resultArray);
?>
Screenshot of infos table:
Just found the solution it s actually adding this before the select query :
mysqli_set_charset($con, "utf8");
$sql = "SELECT lat,lng,`addr` FROM infos";
if ($result = mysqli_query($con, $sql))
{......}

PHP - fputcsv: put JSON values retrieved from MySQL field in separate columns

I have a MySQL column that holds a JSON string of question numbers (0-165) as a key and question answers (score of 1-5) as a value. I need to retrieve all of the MySQL columns, put them into a .csv file with the JSON values in separate columns.
So far, I have a working PHP function that outputs the entirety of my MySQL table to an CSV file using fputcsv(). Here is the code:
function buildcsv(){
$start = $_POST['start'];
$end = $_POST['end'];
$result = mysql_query('SELECT fullname, institution, email, timestamp, responsesJSON
FROM `members` WHERE timestamp BETWEEN "'.$start.'" AND "'.$end.'"');
if (!$result) die('Couldn\'t fetch records');
ob_start();
$fp = fopen('php://output', 'w');
if ($fp && $result) {
while ($row = mysql_fetch_row($result)) {
fputcsv($fp, array_values($row));
}
fclose($fp);
return ob_get_clean();
}else{
$error = "Error. Either there is no data or invalid date.";
return $error;
}
}
And here is the output from the code:
However, I need my CSV output to resemble the following mockup:
Is it possible to do this without changing my database structure? I have seen code on stackoverflow that puts JSON values into separate columns, but how would I integrate it into my existing code which retrieves all other MySQL columns?
Thank you all in advance.
Try this way
while ($row = mysql_fetch_row($result)) {
$marks = json_decode($row['responsesJSON'],true);
unset($row['responsesJSON']);
fputcsv($fp, array_merge(array_values($row),array_values($marks)));
}
That is not best solution, but just food for thought and point to start.
Hope it could help
Much thanks to #KimAlexander and #RocketHazmat's suggestions, I figured it out:
function buildcsv(){
$start = $_POST['start'];
$end = $_POST['end'];
$result = mysql_query('SELECT fullname, institution, email, timestamp, responsesJSON
FROM `members` WHERE timestamp BETWEEN "'.$start.'" AND "'.$end.'"');
if (!$result) die('Couldn\'t fetch records');
ob_start();
$fp = fopen('php://output', 'a');
if ($fp && $result) {
while ($row = mysql_fetch_assoc($result)) {
$personal = array($row['fullname'], $row['institution'], $row['email'], $row['timestamp']);
$scores = json_decode($row['responsesJSON'], true);
fputcsv($fp, array_merge($personal, $scores));
}
fclose($fp);
return ob_get_clean();
}else{
$error = "Error. Either there is no data or invalid date.";
return $error;
}
}

Categories