简体   繁体   English

将JSON数据导入MySQL

[英]Import JSON data to MySQL

I have been trying for months to get this to work, but as im not that experienced with PHP i havent had much luck doing this from scratch. 我已经尝试了几个月才能使这个工作,但是因为我没有经验丰富的PHP,我没有从头开始做这件事。

I am trying to import a generated JSON file into MySQL with the below code but keep getting errors, i believe its where the fields from the JSON arent matching with the import data... some of the JSON fields arent provided so each line varies (not sure how to dynamically create that). 我试图使用下面的代码将生成的JSON文件导入MySQL,但不断出现错误,我相信JSON中的字段与导入数据不匹配...某些JSON字段未提供,因此每行都有所不同(不知道如何动态创建)。 Also the SQL is open to injection, not sure how to fix that? SQL也是开放注入的,不知道如何解决这个问题?

<?php
// open mysql connection
$host = "localhost";
$username = "mysql_username";
$password = "mysql_password";
$dbname = "jsonimport";
$con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));

// use prepare statement for insert query
$st = mysqli_prepare($con, 'INSERT INTO emp(x, y, z, lat, lon, id, color0, color1, color2, color3, color4, color5, tamedAtTime, tamedTime, tribe, tamer, name, baseLevel, health, stamina, oxygen, food, weight, melee, speed, tamed, experience) VALUES (?, ?, ?)');

// bind variables to insert query params
mysqli_stmt_bind_param($st, 'sss', $x, $y, $z, $lat, $lon, $id, $color0, $color1, $color2, $color3, $color4, $color5, $tamedAtTime, $tamedTime, $tribe, $tamer, $name, $baseLevel, $health, $stamina, $oxygen, $food, $weight, $melee, $speed, $tamed, $experience);

// read json file
$filename = 'data.json';
$json = file_get_contents($filename);   

//convert json object to php associative array
$data = json_decode($json, true);

// loop through the array
foreach ($data as $row) {
    // get the dino details
$x = $row['x'];
$y = $row['y'];
$z = $row['z'];
$lat = $row['lat'];
$lon = $row['lon'];
$id = $row['id'];
$color0 = $row['color0'];
$color1 = $row['color1'];
$color2 = $row['color2'];
$color3 = $row['color3'];
$color4 = $row['color4'];
$color5 = $row['color5'];
$tamedAtTime = $row['tamedAtTime'];
$tamedTime = $row['tamedTime'];
$tribe = $row['tribe'];
$tamer = $row['tamer'];
$name = $row['name'];
$baseLevel = $row['baseLevel'];
$health = $row['health'];
$stamina = $row['stamina'];
$oxygen = $row['oxygen'];
$food = $row['food'];
$weight = $row['weight'];
$melee = $row['melee'];
$speed = $row['speed'];
$tamed = $row['tamed'];
$experience = $row['experience'];

    // execute insert query
    mysqli_stmt_execute($st);
}

//close connection
mysqli_close($con);
?>

Import Code: 进口代码:

{"x":205686.734375,"y":57330.2734375,"z":-9629.2021484375,"lat":57.2,"lon":75.7,"id":110197191702290902,"tamed":true,"team":1418527958,"female":true,"color0":14,"color4":14,"color5":14,"tamedAtTime":1166123.9561051205,"tamedTime":3275684.0438948795,"tribe":"DattoSSS","name":"Dattoss","imprinter":"Syn","baseLevel":196,"wildLevels":{"health":28,"stamina":29,"oxygen":31,"food":21,"weight":26,"melee":36,"speed":24},"fullLevel":216,"tamedLevels":{"health":5,"melee":15},"experience":14006.5849609375,"imprintingQuality":0.8208027482032776}

Also is there a way to parse a whole folder of JSON files and import? 还有一种方法来解析整个JSON文件的文件夹并导入? For example each file is ObjectName_Character_BP_C.json is there a way to send the data from each json file to its table witht he same name? 例如,每个文件都是ObjectName_Character_BP_C.json有没有办法将每个json文件中的数据发送到其同名的表中? eg parse Angler_Character_BP_C.json, and them import into jsonimport.Angler_Character_BP_C (mysql table) 例如解析Angler_Character_BP_C.json,然后导入jsonimport.Angler_Character_BP_C(mysql表)

please let me know if you need additional data ... im so lost here :( 如果您需要其他数据,请告诉我...我在这里丢失了:(

You need one '?' 您必须有一个 '?' in the prepare statement for each variable in the bind statement. 在bind语句中每个变量的prepare语句中。 You have only 3 '?', so it won't work the way it is. 你只有3'?',所以它不会像现在这样工作。

Try simpler experiments with 2 or 3 variables to see how it works. 尝试使用2或3个变量进行更简单的实验,看看它是如何工作的。 Then expand when you get it. 然后在你得到它时展开。 You probably need 'd' in the bind for doubles, not 's' for string. 你可能在绑定中需要'd'来表示双打,而不是's'表示字符串。 eg; 例如;

  $st = mysqli_prepare($con, 'INSERT INTO emp (x, y, z) values (?, ? , ?);
  mysqli_stmt_bind_param($st, 'ddd', $x, $y, $z);

You can also use variable names in the prepare statement for the table name, so you can import data from different files and set the table name as needed. 您还可以在prepare语句中为表名使用变量名,这样您就可以从不同的文件导入数据并根据需要设置表名。

Please try with this below code. 请尝试使用以下代码。 Hope it will help you to solve your problem. 希望它能帮助您解决问题。

<?php
function pr($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

$servername = "localhost";
$username = "mysql_username";
$password = "mysql_password";
$dbname = "jsonimport";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$json = '{"0":{"x":205686.734375,"y":57330.2734375,"z":-9629.2021484375,"lat":57.2,"lon":75.7,"id":110197191702290902,"tamed":true,"team":1418527958,"female":true,"color0":14,"color4":14,"color5":14,"tamedAtTime":1166123.9561051205,"tamedTime":3275684.0438948795,"tribe":"DattoSSS","name":"Dattoss","imprinter":"Syn","baseLevel":196,"wildLevels":{"health":28,"stamina":29,"oxygen":31,"food":21,"weight":26,"melee":36,"speed":24},"fullLevel":216,"tamedLevels":{"health":5,"melee":15},"experience":14006.5849609375,"imprintingQuality":0.8208027482032776}}';

//convert json object to php associative array
$data = json_decode($json, true);

// prepare and bind
$stmt = $conn->prepare("INSERT INTO ptero_character_bp_c (`list_x`, `list_y`, `list_z`, `list_lat`, `list_lon`, `list_id`, `list_tamed`, `list_color0`, `list_color1`, `list_color2`, `list_color3`, `list_color4`, `list_color5`, `list_tamedAtTime`, `list_tamedTime`, `list_tribe`, `list_tamer`,`list_name`, `list_baseLevel`, `list_wildLevels_health`, `list_wildLevels_stamina`, `list_wildLevels_oxygen`, `list_wildLevels_food`, `list_wildLevels_weight`, `list_wildLevels_melee`, `list_wildLevels_speed`, `list_experience` ) VALUES (?, ?, ?,?,?,?,?,?,?,?,?, ?, ?,?,?,?,?,?,?,?,?, ?, ?,?,?,?,?)");
$stmt->bind_param("dddddisiiiiiiddsssiiiiiiiid", $x, $y, $z, $lat, $lon, $id, $tamed, $color0, $color1, $color2, $color3, $color4, $color5, $tamedAtTime, $tamedTime, $tribe, $tamer, $name, $baseLevel, $health, $stamina, $oxygen, $food, $weight, $melee, $speed, $experience);

// loop through the array
foreach ($data as $row) {
    // get the dino details
    $x = $row['x'];
    $y = $row['y'];
    $z = $row['z'];
    $lat = $row['lat'];
    $lon = $row['lon'];
    $id = $row['id'];
    $tamed = $row['tamed'];
    $color0 = !empty($row['color0']) ? $row['color0'] : '';
    $color1 = !empty($row['color1']) ? $row['color1'] : '';
    $color2 = !empty($row['color2']) ? $row['color2'] : '';
    $color3 = !empty($row['color3']) ? $row['color3'] : '';
    $color4 = !empty($row['color4']) ? $row['color4'] : '';
    $color5 = !empty($row['color5']) ? $row['color5'] : '';
    $tamedAtTime = $row['tamedAtTime'];
    $tamedTime = $row['tamedTime'];
    $tribe = $row['tribe'];
    $tamer = !empty($row['tamer']) ? $row['tamer'] : '';
    $name = $row['name'];
    $baseLevel = $row['baseLevel'];
    $health = $row['wildLevels']['health'];
    $stamina = $row['wildLevels']['stamina'];
    $oxygen = $row['wildLevels']['oxygen'];
    $food = $row['wildLevels']['food'];
    $weight = $row['wildLevels']['weight'];
    $melee = $row['wildLevels']['melee'];
    $speed = $row['wildLevels']['speed'];
    $experience = $row['experience'];
    // execute insert query
    $stmt->execute();
    echo "New records created successfully";
}

$stmt->close();
$conn->close();

Code Explanation: 代码说明:

$stmt->bind_param("sss", $x, $y, $z);

This function binds the parameters to the SQL query and tells the database what the parameters are. 此函数将参数绑定到SQL查询,并告诉数据库参数是什么。 The "sss" argument lists the types of data that the parameters are. “sss”参数列出了参数所包含的数据类型。 The s character tells mysql that the parameter is a string. s字符告诉mysql该参数是一个字符串。

The argument may be one of four types: 参数可以是以下四种类型之一:

  • i - integer 我 - 整数
  • d - double d - 双倍
  • s - string s - 字符串
  • b - BLOB b - BLOB

You must have one of these for each parameter. 您必须为每个参数都有一个。

By telling mysql what type of data to expect, we minimize the risk of SQL injections. 通过告诉mysql期望什么类型的数据,我们将SQL注入的风险降至最低。

SQLizer does this - just upload your file and hit convert. SQLizer执行此操作 - 只需上传您的文件并点击转换。

You'll receive a SQL file with INSERT statements and TABLE definitions ready to be imported into your database. 您将收到一个带有INSERT语句和TABLE定义的SQL文件,可以将其导入数据库。

It works on CSV,TSV, XML, XLS files too. 它也适用于CSV,TSV,XML,XLS文件。

It's free for files up to 5k rows so you can always try it and see how you get on. 对于长达5k行的文件,它是免费的,因此您可以随时尝试并查看您的使用情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM