I use sdk php bigquery for insert and select data and I add json properties but when I invoke json properties I found null value but in database content data.
this is screen for schema dataset
and this is code php for invoke data from dataset:
$queryJobConfig = $bigQuery->query(
'SELECT id_sso,JSON_value(data) AS test FROM `gsoi-sandbox.users_profil.profil`'
);
$queryResults = $bigQuery->runQuery($queryJobConfig);
foreach ($queryResults as $row) {
$result[] = ($row);
}
result of variable $result is:
my question how to get true data and not null , in dataset I have data and not null
Related
I have data from MySQL database and data from base64 file out of database
I want to combine them so i can have one JSON data with both
$PaFile="JVBERi0xLjcKMS.....";
$NaFile="JVBERi0xLjvvm.....";
$sqlRun = mysqli_query($conn, "SELECT laimCode, laimYear, laimMonth FROM laim_folio");
$row = mysqli_fetch_assoc($sqlRun);
$json_array[] = $row;
$jasondata = json_encode($json_array[]);
What i expect as output is
[{
"laimCode":"1234",
"laimYear":"2021",
"laimMonth":"11",
"PaFile":"JVBERi0xLjcKMS.....",
"NaFile":"JVBERi0xLjvvm....."
}]
If i put these two variable in SQL as static column with value i can get the result i want
But is there way to combine them outside SQL ?
Like extends array with two extra field and then convert to JSON
You can either add them to the array after fetching the data from the database and before adding into the overall array....
$row = mysqli_fetch_assoc($sqlRun);
$row['PaFile'] = "JVBERi0xLjcKMS.....";
$row['NaFile'] = "JVBERi0xLjvvm.....";
$json_array[] = $row;
$jasondata = json_encode($json_array);
Or add the values to the SQL so they become part of the result set. So the value is just a literal and the alias becomes the column name...
SELECT laimCode, laimYear, laimMonth,
"JVBERi0xLjcKMS....." as PaFile,
"JVBERi0xLjvvm....." as NaFile
FROM laim_folio
I have recently used this concat operator to join columns in JSON.
select concat('{"laimCode":',laimCode,',"laimYear":',laimYear,',"laimMonth":',laimMonth,',"PaFile":"JVBERi0xLjcKMS.....", "NaFile":"JVBERi0xLjvvm....."}') as json FROM laim_folio
I want to export a data from database into an excel file. i've tried to use PHPExcel
im trying to get some data with id as the parameter with this code, but the result of the export is showing all data. meanwhile, when i do the var_dump at the '$rowExcel->name', its showing a data based on id (not all data).
$this->db->select('id, name, r_value');
$this->db->where_in('id', $id);
$result = $this->db->get("person");
$rows = 3;
foreach ($result->result() as $rowExcel){
$excel->getActiveSheet()->setCellValueByColumnAndRow(0,$rows,'');
$excel->getActiveSheet()->setCellValueByColumnAndRow(1,$rows,$rowExcel->name);
$rows++;
}
what i want is to export the data with the id as the parameter (not export all data).. btw, the id is an array
How can I list names of tables in sql database and the table columns for the each table and then get a json encoding of the results in PHP?
Here is code to display tables:
$result = mysql_query("show tables"); // run the query and assign the result to $result
while($table = mysql_fetch_array($result)) {
// go through each row that was returned in $result
echo($table[0] . "<BR>"); // print the table that was returned on that row.
}
You could create an array/object and add the data to it. To get a json result just call json_encode on the array/object.
Here is what I would do:
// Create an object
$results = new stdClass();
// Get all tables
$result = mysql_query("show tables");
// Always check if the query returned anything to prevent possible errors
if(mysql_affected_rows() > 0) {
// Loop through each table
while($table = mysql_fetch_assoc($result)) {
// Get the columns in the table
$resultColumns = mysql_query("show columns from ".$table);
if(mysql_affected_rows() > 0) {
// Columns found. Create a new property in the object and assign the columns to it
$tableColumns = mysql_fetch_assoc($resultColumns);
$results[$table] = $tableColumns;
}
else {
// No columns found. Create a new property in the object and assign a blank array.
$results[$table] = [];
}
}
}
Now we can return that data as a json with json_encode($results).
http://php.net/manual/en/function.json-encode.php
As a side note, I would look to use mysqli, not mysql (as it is now depreciated). The syntax for it is basically exactly the same, but you need to pass the connection variable as the first parameter in most cases. So it would be useful to use create a database/connection class and call to those methods, instead of calling mysql or mysqli functions directly.
See here: https://www.w3schools.com/php/php_ref_mysqli.asp
And here (an example): https://www.johnmorrisonline.com/simple-php-class-prepared-statements-mysqli/
Note: This is pseudo code and not tested but should be okay. Let me know if you need any help with it.
So here the thing:
I am developing a backend (php) that connects with Microsoft SQL Server with android application to be the user interface.
However, I am facing a problem when it comes to encoding json with php.
It is more than fair to say that I am a beginner in php.
This is my code:
$result = sqlsrv_query( $conn, 'select * from table1');
$row = sqlsrv_fetch_array($result);
$array = array();
while($row =sqlsrv_fetch_array($result))
{
$array[]=$row;
}
echo json_encode(array("data"=>array_values($array)));
So the table actually has nothing so far:
just two attributes: name and age
The problem here is that the return is being as follows:
{"data":[{"0":"Miriana","name":"Miriana","1":null,"age":null},{"0":"Luke","name":"Luke","1":null,"age":null},{"0":"Sara","name":"Sara","1":null,"age":null},{"0":"Fuss","name":"Fuss","1":20,"age":20}]}
There is always a number preceding the values with a value then the real key and a value.
For example:
"0":"Miriana"
"1":null
Thanks so much for anyone who would check this out.
**I checked those links:
Decode json With PHP not working
Parsing JSON with PHP
Encoding JSON with PHP issues with array
**
You need to specify the fetch type:
while($row =sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
By default you get both the numeric and the associative key.
And you should probably remove the fetch before the loop as you will not get the first row in your current results.
The default fetch type of sqlsrv_fetch_array is SQLSRV_FETCH_BOTH, which fetches the rows as associative and numeric array, so you get both types. If you only want an associative array, set the second argument of sqlsrv_fetch_array to SQLSRV_FETCH_ASSOC.
$result = sqlsrv_query( $conn, 'select * from table1');
$array = array();
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
$array[]=$row;
}
echo json_encode(array("data"=>array_values($array)));
Removed the first fetch, because you will not get the first row if you do that.
Reference: http://php.net/manual/en/function.sqlsrv-fetch-array.php
This code works perfectly for parsing the json object, but I need to store all json data to mysql. How can I retrieve the value to store in mysql?
function getDataAsArray($filename){
$dataArray=array();
$contents = file_get_contents($filename);
$first_split=explode("}}{",$contents);
$json_array=explode("\n",$first_split[0]);
foreach($json_array as $value){
$data = json_decode($value, true);
$dataArray[]=$data;
}
return $dataArray;
}
$logDataArray=getDataAsArray("akshara.txt");
echo "<pre>";print_r($logDataArray); echo "</pre>"
MySQL supports a JSON datatype in 2015, much like Postgres has had for a good few years now. You can simply insert the JSON into your database.
$json = file_get_contents($file); // {"my_value": "my data"}
DB::query("INSERT INTO my_table (json) VALUES ('{$json}')");
You can then use the various JSON functions to query your inserted data.
SELECT JSN_EXTRACT(json, '$.my_value') AS my_value FROM my_table // my_value = my data