将JSON数据导入MySQL

时间:2016-12-24 06:30:45

标签: php mysql json

我已经尝试了几个月才能让这个工作,但是因为我没有经验丰富的PHP,我从没有从头开始做这件事。

我正在尝试使用以下代码将生成的JSON文件导入MySQL,但不断出现错误,我相信JSON中的字段与导入数据不匹配...某些JSON字段未提供,因此每个行变化(不确定如何动态创建)。 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);
?>

导入代码:

{"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表)

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

3 个答案:

答案 0 :(得分:0)

你需要一个&#39;?&#39;在bind语句中每个变量的prepare语句中。你只有3&#39;?&#39;,所以它不会按照它的方式工作。

尝试使用2个或3个变量进行简单的实验,看看它是如何工作的。然后在你得到它时展开。你可能需要&#39; d&#39;在双打的约束中,不是&#39; s&#39;对于字符串。  例如;

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

您还可以在prepare语句中为表名使用变量名,这样您就可以从不同的文件导入数据并根据需要设置表名。

答案 1 :(得分:0)

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

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

此函数将参数绑定到SQL查询,并告诉数据库参数是什么。 &#34; sss&#34;参数列出参数所包含的数据类型。 s字符告诉mysql该参数是一个字符串。

论证可能是以下四种类型之一:

  • i - 整数
  • d - double
  • s - string
  • b - BLOB

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

通过告诉mysql期望什么类型的数据,我们将SQL注入的风险降至最低。

答案 2 :(得分:0)

SQLizer执行此操作 - 只需上传您的文件并点击转换。

您将收到一个带有INSERT语句和TABLE定义的SQL文件,可以将其导入数据库。

它也适用于CSV,TSV,XML,XLS文件。

对于最多5k行的文件,它是免费的,所以你总是可以尝试它,看看你是如何进行的。