gpt4 book ai didi

php - 将 JSON 数据导入 MySQL

转载 作者:行者123 更新时间:2023-11-30 22:07:06 27 4
gpt4 key购买 nike

几个月来我一直在努力使它工作,但由于我对 PHP 的经验不多,所以我运气不好从头开始做这件事。

我正在尝试使用以下代码将生成的 JSON 文件导入 MySQL,但不断出现错误,我相信它来自 JSON 的字段与导入数据不匹配的地方......没有提供一些 JSON 字段,所以每个行各不相同(不确定如何动态创建它)。此外,SQL 对注入(inject)开放,不确定如何解决?

<?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);
?>

导入代码:

{"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}

还有没有办法解析整个文件夹的 JSON 文件并导入?例如,每个文件都是 ObjectName_Character_BP_C.json 有没有办法将每个 json 文件中的数据发送到同名的表中?例如解析 Angler_Character_BP_C.json,并将它们导入到 jsonimport.Angler_Character_BP_C(mysql 表)

如果您需要其他数据,请告诉我......我在这里迷路了:(

最佳答案

请尝试使用以下代码。希望它能帮助您解决问题。

<?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();

代码说明:

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

此函数将参数绑定(bind)到 SQL 查询并告诉数据库参数是什么。 “sss”参数列出参数的数据类型。 s 字符告诉 mysql 该参数是一个字符串。

参数可以是四种类型之一:

  • i - 整数
  • d - 双
  • s - 字符串
  • b - BLOB

每个参数都必须有其中之一。

通过告诉 mysql 需要什么类型的数据,我们可以最大限度地降低 SQL 注入(inject)的风险。

关于php - 将 JSON 数据导入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com