我有以下示例json文件,我试图用它填充MySQL表。使用json数据填充书籍表不起作用且没有错误。我想知道以下PHP代码中的错误:
<?php
$connect=new PDO('mysql:host=localhost;dbname=books','root','0000');
$filename= "data.json";
$data=file_get_contents($filename);
$array=json_decode($data,true);
$stmt=$connect->prepare("INSERT INTO books values(?,?,?,?,?,?,?,?,?)");
foreach($array["books"] as $row){
$stmt->bindParam(1,$row['isbn']);
$stmt->bindParam(2,$row['title']);
$stmt->bindParam(3,$row['subtitle']);
$stmt->bindParam(4,$row['author']);
$stmt->bindParam(5,$row['published']);
$stmt->bindParam(6,$row['publisher']);
$stmt->bindParam(7,$row['pages']);
$stmt->bindParam(8,$row['description']);
$stmt->bindParam(9,$row['website']);
$stmt->execute();
}
?>
json文件看起来像这样:
{
"books":[
{
"isbn":"9781593275846",
"title":"Eloquent JavaScript, Second Edition",
"subtitle":"A Modern Introduction to Programming",
"author":"Marijn Haverbeke",
"published":"2014-12-14T00:00:00.000Z",
"publisher":"No Starch Press",
"pages":472,
"description":"JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
"website":"http://eloquentjavascript.net/"
},
{
"isbn":"9781449331818",
"title":"Learning JavaScript Design Patterns",
"subtitle":"A JavaScript and jQuery Developer's Guide",
"author":"Addy Osmani",
"published":"2012-07-01T00:00:00.000Z",
"publisher":"O'Reilly Media",
"pages":254,
"description":"With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
"website":"http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/"
} ] }
Mysql表结构如下所示:
-- Table structure for table
books` CREATE TABLE books
(
isbn
int(11)NOT NULL,
title
varchar(50)NOT NULL,
subtitle
varchar(50)NOT NULL,
author
varchar(50)NOT NULL,
published
日期非空,
publisher
varchar(50)NOT NULL,
pages
int(11)NOT NULL,
description
文本NOT NULL,
website
varchar(255)NOT NULL
)ENGINE = MyISAM DEFAULT CHARSET = latin1;
答案 0 :(得分:2)
添加一些错误报告,以查看出现错误的原因,并首先绑定变量,然后在foreach循环中设置它们并执行查询
try {
$connect=new PDO('mysql:host=localhost;dbname=books','root','0000');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$connect->prepare("INSERT INTO books (isbn,title,subtitle,author,published,publisher,pages,description,website) values(:isbn,:title,:subtitle,:author,:published,:publisher,:pages,:description,:website)");
$filename= "data.json";
$data=file_get_contents($filename);
$array=json_decode($data,true);
$stmt->bindParam(':isbn', $isbn);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':subtitle', $subtitle);
$stmt->bindParam(':author', $author);
$stmt->bindParam(':published', $published);
$stmt->bindParam(':publisher', $publisher);
$stmt->bindParam(':pages', $pages);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':website', $website);
foreach($array["books"] as $row){
$isbn = $row['isbn'];
$title = $row['title'];
$subtitle = $row['subtitle'];
$author = $row['author'];
$published = $row['published'];
$publisher = $row['publisher'];
$pages = $row['pages'];
$description = $row['description'];
$website = $row['website'];
$stmt->execute();
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
答案 1 :(得分:0)
对于其他绊脚石的人,上面的Dimitris代码中有一个小错误,并且编辑队列已满。
“ try”块已打开但未关闭,因此我添加了一个大括号:
try {
$connect=new PDO('mysql:host=localhost;dbname=books','root','0000');
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$connect->prepare("INSERT INTO books (isbn,title,subtitle,author,published,publisher,pages,description,website) values(:isbn,:title,:subtitle,:author,:published,:publisher,:pages,:description,:website)");
$filename= "data.json";
$data=file_get_contents($filename);
$array=json_decode($data,true);
$stmt->bindParam(':isbn', $isbn);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':subtitle', $subtitle);
$stmt->bindParam(':author', $author);
$stmt->bindParam(':published', $published);
$stmt->bindParam(':publisher', $publisher);
$stmt->bindParam(':pages', $pages);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':website', $website);
foreach($array["books"] as $row){
$isbn = $row['isbn'];
$title = $row['title'];
$subtitle = $row['subtitle'];
$author = $row['author'];
$published = $row['published'];
$publisher = $row['publisher'];
$pages = $row['pages'];
$description = $row['description'];
$website = $row['website'];
$stmt->execute();
}
} // Added this brace to close the 'try' opening brace
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}