-
-
Save bilalinamdar/dcfb1d83dae1d8f97ee15b3bc03fe0d5 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