Skip to content

Instantly share code, notes, and snippets.

@lodow
Last active August 6, 2021 18:04
Show Gist options
  • Save lodow/7b89e92cc00fe0c50281 to your computer and use it in GitHub Desktop.
Save lodow/7b89e92cc00fe0c50281 to your computer and use it in GitHub Desktop.
Script to import json to mysql in php
<?php
$content = json_decode(file_get_contents("filename.json")), true);
mysql_connect('localhost', 'user', 'pass');
mysql_select_db('db');
foreach($content as $item) {
$columns = implode(", ",array_keys($item));
$escaped_values = array_map('mysql_real_escape_string', array_values($item));
$values = "'".implode("', '", $escaped_values)."'";
$sql = "INSERT INTO `tbl_name`($columns) VALUES ($values)";
mysql_query($sql);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment