我正在尝试读取json数据并将其插入到mysql中, 但是所有列似乎都与值匹配。
以下查询中我错了:
gmrjson.json:
<?php
//First: read data
$fo=fopen("gmrjson.json","r");
$fr=fread($fo,filesize("gmrjson.json"));
$array=json_decode($fr,true);
//Second: create $values
$rows = array();
foreach ($array['rtVisitInfoOpObjV1'] as $key => $value)
$rows[] = "('" . $key . "', '" . $value . "')";
$values = implode(",", $rows); //echo '<pre>';print_r($values);die;
//Save to DB
$hostname = 'localhost';
$username = 'root';
$password = '';
try
{
$dbh = new PDO("mysql:host=$hostname;dbname=nodejs", $username, $password);
echo 'Connected to database<br />';
echo "INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES " . $values;
$count = $dbh->exec("INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES " . $values) or die(print_r($dbh->errorInfo(), true));
echo $count; die;
$dbh = null;
echo 'Success<br />';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
sample.php:
Array
(
[rtVisitInfoOpObjV1] => Array
(
[version] => 1
[visitorId] => vis1
[dwellTime] => 0
[poiId] => poi1
[srId] => sr1
[zoneId] => zone1
[poiProximityConfidence] => 0
[zoneProximityConfidence] => 0
[poiPresenceConfidence] => 15
[zonePresenceConfidence] => 0
[normalizedTime] => 1489574975000
)
)
Connected to database
INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ('version', '1'),('visitorId', 'vis1'),('dwellTime', '0'),('poiId', 'poi1'),('srId', 'sr1'),('zoneId', 'zone1'),('poiProximityConfidence', '0'),('zoneProximityConfidence', '0'),('poiPresenceConfidence', '15'),('zonePresenceConfidence', '0'),('normalizedTime', '1489574975000')Array
(
[0] => 21S01
[1] => 1136
[2] => Column count doesn't match value count at row 1
)
以下是mysql表的描述
UPDATE-调试生成的查询:
radioButtons[i].Anchor = AnchorStyles.Left;
radioButtons[i].Anchor = AnchorStyles.Bottom;
答案 0 :(得分:3)
您可以看到您的SQL错误:
INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ('version', '1'),('visitorId', 'vis1'),('dwellTime', '0'),('poiId', 'poi1'),('srId', 'sr1'),('zoneId', 'zone1'),('poiProximityConfidence', '0'),('zoneProximityConfidence', '0'),('poiPresenceConfidence', '15'),('zonePresenceConfidence', '0'),('normalizedTime', '1489574975000') ...
必须是:
INSERT INTO gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES (1, 'vis1', '0','poi1','sr1' ...)
你应该删除数字值
周围的单引号答案 1 :(得分:0)
您的查询实际上是提供一对值而不是单个值
而不是这样做:
foreach ($array['rtVisitInfoOpObjV1'] as $key => $value)
$rows[] = "('" . $key . "', '" . $value . "')";
$values = implode(",", $rows); //echo '<pre>';print_r($values);die;
这样做
foreach ($array['rtVisitInfoOpObjV1'] as $key => $value)
array_push($rows, $value);
$values = implode(",", $rows); //echo '<pre>';print_r($values);die;