【发布时间】:2017-11-23 22:40:59
【问题描述】:
我正在使用来自 JSON 文件的 PDP Prepared PDO 语句在 SQL 数据库中插入值。
这就是我的代码:
$requestno = 8
$maxrequest = 15
while ($requestno < $maxrequest)
{
$response = file_get_contents("https://api.themoviedb.org/3/movie/".$requestno."?api_key=456cec7xxxxxxxxxxxxxe0c834a");
if ($response != FALSE) {
$response = json_decode($response, true);
}
$requestno++;
<?php
try {
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO TABLE (firstname)
VALUES (:firstname)");
$stmt->bindParam(':firstname', $firstname);
// insert a row
if ( $response["firstname"] != "" )
$tagline = $response["firstname"];
$stmt->execute();
echo "New records created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
}
?>
您可以看到 JSON 文件的 url 包含“.$requestno.”,所以我正在从多个 JSON 文件中检索数据。
那么这里会发生什么,如果 JSON 文件中的 firstname 为空,MYSQL 会插入之前的 JSON 文件 firstname。如何解决?
如果您需要更多信息,请告诉我。
【问题讨论】:
-
您自己尝试过什么解决方法?你试过在
file_get_contents之前设置$response = false吗? -
在循环开始时清空变量。
$firstname = '';和 ^^ -
所有 JSON 文件都有一定的价值。所以,它永远不会是假的
-
让我试试 Jon 的解决方案