gpt4 book ai didi

php - 使用 PDO 时无法将标量值用作数组

转载 作者:行者123 更新时间:2023-11-29 07:15:08 24 4
gpt4 key购买 nike

我尝试将数据从 JSON API 输入到 MySQL 数据库,但出现错误。

这是错误。

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 8

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 9

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 10

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 11

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 12

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 13

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 14

Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\simplehtmldom\example\scraping\example_scraping_imdb.php on line 15

所有这些行都是 P​​HP 代码中的绑定(bind)参数语句。

这是我使用的代码(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

希望有人能解决。提前致谢。

最佳答案

根据documentation :

Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement that was used to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

您需要创建自变量列表以将它们绑定(bind)到语句中:

foreach ($data as $row) {
$v1 = $row['hits']['hits']['_id'];
$v2 = $row['hits']['hits']['fields']['name'];
$v3 = $row['hits']['hits']['fields']['start'];
...
$stmt->bindParam(1, $v1);
$stmt->bindParam(2, $v2);
$stmt->bindParam(3, $v3);
...
$stmt->execute();
}

或者您可以尝试使用 bindValue相反:

foreach ($data as $row) {
$stmt->bindValue(1, $row['hits']['hits']['_id']);
$stmt->bindValue(2, $row['hits']['hits']['fields']['name']);
$stmt->bindValue(3, $row['hits']['hits']['fields']['start']);
...
$stmt->execute();
}

更新 在查看了您提供的 json 后,我没有那么多话要说。 我不知道您的期望是什么,但您绝对不能针对该数据执行 foreach ($data as $row) { 操作。

您可以玩一下您的数据 here

因此,要获取 _id,您应该调用:

$data[1]['hits']['hits'][1]['_id'];

要获取字段,您需要:

$data[1]['hits']['hits'][0]['fields'];

您使用的数据可能与您在此处发布的数据不同。

UPDATE 2 SO不是为你开发。下次请先尝试做作业。但是here我猜这就是您正在寻找的:

foreach ($data[1]['hits']['hits'] as $row) {
$v1 = $row['_id'];
$v2 = $row['fields']['name'][0];
$v3 = $row['fields']['start'][0];
....
}

关于php - 使用 PDO 时无法将标量值用作数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com