Skip to content
Advertisement

Populating mysql table with json data in phpmyadmin

I have the following example json file and I’m trying to populate a MySQL table with it. Populating books table with json data did not work and with no errors. I want to know what is wrong in the following PHP code:

    <?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 file looks like this:

 {
   "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 looks like this:

--- 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` date NOT NULL,
  `publisher` varchar(50) NOT NULL,
  `pages` int(11) NOT NULL,
  `description` text NOT NULL,
  `website` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Table structure

Advertisement

Answer

Add some error reporting to see what if there is an error and also first bind the variables and then set them in foreach loop and execute the query

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();
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement