Skip to content

Instantly share code, notes, and snippets.

@sleepypioneer
Created January 5, 2018 08:28
Show Gist options
  • Save sleepypioneer/203f0f9a6b5777a5ca7289a9a99efcce to your computer and use it in GitHub Desktop.
Save sleepypioneer/203f0f9a6b5777a5ca7289a9a99efcce to your computer and use it in GitHub Desktop.
php import from JSON (converted to values array) and inserted to php database - Book Club app
<?php
$jsondata = file_get_contents("books.json");
$json = json_decode($jsondata, true);
include('dbconnection.php');
foreach($json['books'] as $key => $value){
$array = array();
foreach($value as $prop){
array_push($array, "'". $prop ."'");
}
$a = implode(' ,', $array);
$query="INSERT INTO books(id,cover_image,title,author,rating,length,shelf,amazon_link,date_added,date_read,reviews)VALUES($a)";
$run_query = mysqli_query($connection, $query);
if($run_query){
echo "Data has been inserted into your Database.";
} else {
echo "Data could not be inserted";
}
echo $a;
}
mysqli_close($connection);
?>
@sleepypioneer
Copy link
Author

Pretty chuffed to have figured this out super quickly this morning and automate transfering the Data from a JSON to database, although of course it is not so much, who needs extra typing or copy pasting :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment