Created
February 24, 2013 17:32
-
-
Save amin007/5024721 to your computer and use it in GitHub Desktop.
Berikut adalah contoh data dari json kepada tatasusunan php dan masuk terus dalam mysql
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 | |
// sumber - http://www.foscode.com/json-array-single-mysql-insert-update/ | |
//First decode the array | |
$arr = file_get_contents('sql_json.php'); | |
$decarr = json_decode($arr, true); | |
//echo '<pre>$decarr->'; print_r($decarr) . '</pre>'; | |
$count = count($decarr['rows']); | |
$values = array(); // This will hold our array values so we do one single insert | |
for ($x=0; $x < $count; $x++) | |
{ | |
$newrec = $decarr['rows'][$x]; | |
$id = $newrec['id']; | |
$name = $newrec['name']; | |
$author = $newrec['author']; | |
// Create insert array | |
$values[] = "('".$id."', '".$name."', '".$author."')"; | |
} | |
// Insert the records | |
$sql = "INSERT INTO book (id,name,author) | |
VALUES ".implode(",\r", $values)." "; | |
echo '<pre>'; print_r($sql) . '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment