Create . JSON file based on the result of the SQL query

Asked

Viewed 99 times

0

I am trying to send data from my database to a . json file but I am not able to because it creates the file and nothing appears, only the empty array.

my code is as follows::

<?php
$connect = mysqli_connect("localhost","root","admin","opcuaserver");
$query = "SELECT * FROM sensores (Timestamp, Metros, LM335, LM35)";
$result = mysqli_query($connect, $query);

$data = array();
$data[$result] = array();


//format the data
$formattedData = json_encode($data);

//set the filename
$filename = 'basedadossensores.json';

//open or create the file
$handle = fopen($filename,'w+');

//write the data into the file
fwrite($handle,$formattedData);

//close the file
fclose($handle);

?>

and the result when I run the code

{"Values->":[]}
  • The $result returns what ?

  • I want to return the values that are in the database

  • yes but now it is returning what ? if you run this query directly in the database it returns what ?

  • at this point when I run the file it gets connected in my database and shows nothing I’m trying to go by creating an array but I think I’m fooling myself in his programming

  • Let me query SELECT * FROM sensores (Timestamp, Metros, LM335, LM35) what are these meters, LM335 ?

1 answer

0


I made him walk the $result to generate a vector to be converted by the function json_encode(); work:

<?php
$connect = mysqli_connect("localhost","root","admin","opcuaserver");
$query = "SELECT Timestamp, Metros, LM335, LM35 FROM sensores";
$result = mysqli_query($connect, $query);
if ($result) {
    $data = array();
    while($row = mysqli_fetch_array($result,  MYSQLI_ASSOC)) {
            $data[] = $row;
    }
} else  {
    echo "erro na query";
}

//format the data
$formattedData = json_encode($data);

//set the filename
$filename = 'basedadossensores.json';

//open or create the file
$handle = fopen($filename,'w+');

//write the data into the file
fwrite($handle,$formattedData);

//close the file
fclose($handle);

?>

Test there and see if it will work as you need it. I followed this reference.

  • the result was this -> error in query Notice: Undefined variable: data in C: xampp htdocs files basesensores.php on line 14

  • Test again I ended up forgetting about Else, but I think the problem is in your Sql query, what are these values here ? (Timestamp, Metros, LM335, LM35)

  • Timestamp is the time and the date the rest are values of temperature and proximity sensors

  • Are the search columns ? for example Meters is a column ?

  • the resultad is this -> Warning: Use of Undefined Constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a Future version of PHP) in C: xampp htdocs files basesensores.php on line 6 Warning: mysqli_result::fetch_array() expects Parameter 1 to be int, string Given in C: xampp htdocs files basesensores.php on line 6 Notice: Undefined variable: data in C: xampp htdocs files basesensores.php on line 14

  • yes are the search columns

  • I edited the query for what I assume to be tested again,

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.