Last active
August 6, 2021 18:04
-
-
Save lodow/7b89e92cc00fe0c50281 to your computer and use it in GitHub Desktop.
Script to import json to mysql in php
This file contains hidden or 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 | |
$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