Following is my code
<?php
$json = '{"apples":"green","bananas":"yellow"}';
$var=(json_decode($json, true));
print_r($var);
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the eventbase
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a eventbase to work with
$selected = mysql_select_db("json",$dbhandle)
or die("Could not select json");
// Insert $event array into eventbase
foreach ($json as $key => $value) {
$db_insert = mysql_query("INSERT INTO fruits (fruit,color) VALUES" . $value);
mysql_query($db_insert);
if (!$db_insert)
{
die('Could not connect - event insert failed: ' . mysql_error());
}
}
?>
New to JSON and PHP, please post any changes I can do to insert those records into MySQL.
However, another part remains. I want to save the JSON encoded data into MySQL in string format. How can I do so?