Skip to content

Instantly share code, notes, and snippets.

@amin007
Created February 24, 2013 17:32
Show Gist options
  • Save amin007/5024721 to your computer and use it in GitHub Desktop.
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
<?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