【发布时间】:2014-02-07 08:39:49
【问题描述】:
以下是我的代码
<?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());
}
}
?>
JSON 和 PHP 新手,请发布我可以做的任何更改以将这些记录插入 MySQL。
但是,另一部分仍然存在。我想将 JSON 编码的数据以字符串格式保存到 MySQL 中。我该怎么做?
【问题讨论】: