Created
January 5, 2018 08:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)