I need some help, I can't get it done...
I have a database, called "db"
Inside I have a table named "objects"
In this table there is a column named "details" which contain a JSON (see image)
In a PHP script I want to edit the "cost" parameters based on the value of the "type" and the other parameter named "source" to keep his value.
For example, if type is small -> cost = 1
type = big -> cost = 2.
Any ideas?
I have tried several but I received warning:
Warning: Attempt to assign property of non-object
Notice: Array to string conversion
To edit the JSON data in the details column you would have to first decode the JSON like so:
$query //This be equal to your query data from the db containing the table data
$json = $query["details"];
$d_coded_json = json_decode($json);
if($d_coded_json->type == "small"){
$d_coded_json->cost = 1;
}else if ($d_coded_json->type == "big"){
$d_coded_json->cost = 2;
}
// you can convert to back to json by running the code below
$new_json = json_encode($d_coded_json);
// save back to db
Related
I've created a form that lets users input measured temperatures at a specific interval, let's say 0 minutes and 1 minutes. This data is sent through by an AJAX call to our server, which in turn returns a function to handle the request.
The function will retrieve the target data from the database - stored as a JSON. When the required data is found, the function will 'unpack' the JSON and alter the specific data. However, eventhough I need the user input to be stored in an associative array, somehow the input gets saved as a regular array.
The specific code that alters the data looks something like underneath:
foreach($list as $list => &$items){
if(isset($items[$item_id])){
if($temperature != ''){
$items[$item_id][$temp_type][(string) $minute] = (float)$temperature;
}else{
unset($items[$item_id][$temp_type][(string) $minute]);
}
}
}
The data that is sent through looks something like this:
AJAX CALL 1
$item_id = 101;
$temp_type = 'hot';
$minute = (int)0;
$temperature = (float)26.5;
AJAX CALL 2
$item_id = 101;
$temp_type = 'hot';
$minute = (int)1;
$temperature = (float)55.5;
I'm expecting the following output in my database:
"101":{"hot":{"0":26.5,"1":55.5},
However, what I receive is:
"101":{"hot":[26.5,55.5],
Important info:
Whenever I send more than 2 AJAX requests to the server - i.e. when the user form contains more than 2 fields ranging between 0 - X minutes - the associative array keeps its form and everything is just fine.
So what I don't quite get at the moment, is why $items[$item_id][$temp_type][(string) $minute] = (float)$temperature; reverts the data back to a regular array?
I am storing data in json format in mysql database table column like
table A
column-user_details
"{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}"
I am fetching data like
json_decode($variable, true);
<?php echo $variable['name'];?>
However I am getting error like
Illegal string offset 'name'
the first parameter of json_decode is not a parameter passing by reference, so if you want to get the json code as php array, you mast adding the result of json_decode into a variable.
<?php
$variable = "{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}";
$array = json_decode($variable, true);
echo $array['name'];
NB: json_decode() is a php code, so you must use it after <?php tag, not outside of it
In database JSON data is stored in below way:
{"name":"abc","score":5}
Now everytime I want to add json object with different name and score to same json data as :
{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}
Right now I have tried this code
$database_data = $row['JSONstring']; //fetched json string from database
$json_decode_data = json_decode($database);
array_push($json_decode_data, $new_json_string); //data to append to database data
$updated_json_data = json_encode($json_decode_data); // at last encode the update json data
But this don't work and throws a warning : Parameter 1 is an object.
How do solve it ?
Now with all the answer and comments given here I got the required answer. So I appreciate all your help.
In my question starting, the first JSON String format is invalid:
{"name":"abc","score":5}
Instead of this, it would be :
[{"name":"abc","score":5}]
Required JSON data's format would be:
[{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}]
And this is achieved by array_merge() function as follows:
Code to append json object to json object string
//fetched json string from database
ie.[{"name":"abc","score":5}]
$database_data = $row['JSONstring'];
//decode both string the database data string and the new one which is to be merged
$json_decode_database_data = json_decode($database_data, true);
$json_decode_new_data = json_decode($database_data, true);
//merge both the decoded array by array_merge()
$required_json_data = array_merge($json_decode_data, $new_json_string);
//at last encode required json data
$updated_json_data = json_encode($required_json_data);
// You will get required json data
[{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}]
I wanted to save the data in this format into database option field:
{"type":"type_value"}
I am getting data in post varaibles like this
$type_value = $request->input('type_value');
$type = $request->input('type');
How Can I save this in database?
I have tried this
$data['options'] = array($type,$type_value);
But by this it is saving in this format:
["Qualifiers","1"]
I even tried doing this:
$data['options'] = json_encode(array($type,$type_value));
Instead it is saving like this
"[\"Qualifiers\",\"1\"]"
how can I do this?
You just have to change your array definition. Your array considers 2 different elements i.e type and type_value. So just make your array with key value pair and you are all set.
json_encode(array($type => $type_value))
Check this :- Fiddle
I know this question has been asked a lot and I have researched, trust me.
I have a Json file with multi variables in it. I want to take that Json file and store it in a single BLOB field on MySql Database.
I did json_decode() but when I try to store it, PHP gives me an error because json_decode() returns a php array but bindParam wants a string varaible.
I tried implode(",", json_decode($data,true)) but it didn't work.
Can you help me please...
My JSON file looks like this;
[
{ "xyz": "abc",
"izd": 1
},
{ "xyz": "abc",
"izd": 1
},
{ "xyz": "abc",
"izd": 1
}]
My PHP code is like this;
$json = $_POST["jsonfile"];
$jsondata = json_decode($json,true);
$implodedjsondata = implode(",",$jsondata); // In here, php gets error in impode ( Error : Notice: Array to string conversion)
$Query = $conn->prepare("INSERT INTO table1(somedata) VALUES(:data)");
$Query->bindParam(':data',$implodedjsondata); // In case of not using implode, this line gets error( Error : Notice: Array to string conversion)
$Query->execute();
Thanks...
its already a json? don't do anything with it, insert it as-is. and use bindValue, bindParam is an optimization thing when you have a lot of data you need to change fast when loop-executing a statement..
$json = $_POST["jsonfile"];
$Query = $conn->prepare("INSERT INTO table1(somedata) VALUES(:data)");
$Query->bindValue(':data',$json); // In case of not using implode, this line gets error( Error : Notice: Array to string conversion)
$Query->execute();