【发布时间】:2016-11-08 22:50:56
【问题描述】:
我正在尝试将 JSON API 中的数据输入 MySQL 数据库,但出现错误。
这是错误。
警告:不能在第 8 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 9 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 10 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 11 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 12 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 13 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 14 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
警告:不能在第 15 行的 C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php 中将标量值用作数组
所有这些行都是 PHP 代码中的绑定参数语句。
这是我使用的代码(JSON 对象是嵌套的):
<?php
$db = new PDO('mysql:host=localhost;dbname=wtd','root','');
$jsondata = file_get_contents('https://hugo.events/genre/pop/next/200/100');
$data = json_decode($jsondata, true);
$stmt = $db->prepare("insert into events values(?,?,?,?,?,?,?,?)");
foreach ($data as $row) {
$stmt->bindParam(1, $row['hits']['hits']['_id']);
$stmt->bindParam(2, $row['hits']['hits']['fields']['name']);
$stmt->bindParam(3, $row['hits']['hits']['fields']['start']);
$stmt->bindParam(4, $row['hits']['hits']['fields']['venue.location']);
$stmt->bindParam(5, $row['hits']['hits']['fields']['description']);
$stmt->bindParam(6, $row['hits']['hits']['fields']['header']);
$stmt->bindParam(7, $row['hits']['hits']['fields']['logo']);
$stmt->bindParam(8, $row['hits']['hits']['fields']['genres']);
$stmt->execute();
}
编辑:JSON 数据示例:
https://ghostbin.com/paste/437q7
希望有人有解决方案。提前致谢。
【问题讨论】:
-
我对 PDO 很陌生,所以我不明白他们的意思
-
与其说“你必须访问这个 URL”,不如发布一个数据样本?
-
已编辑。它有一个例子 niw