将json数组插入mysql数据库

时间:2022-09-26 07:51:42

Hi I'm trying to insert the json array into my MySQL database I'm new for this php development i have seen so many methods that all little confusing please help me out.

嗨我正在尝试将json数组插入我的MySQL数据库我是这个php开发的新手我已经看到了很多方法,所有有点混乱请帮帮我。

This is my json data.

这是我的json数据。

[{"name":"0","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"13123123","phone":"sdfsdfdsfsd","city":"sdfsf","email":"13123123"}]

[{ “Name”: “0”, “手机”: “DSF”, “城市”: “sdfsdf”, “电子邮件”: “DSF”},{ “名”: “13123123”, “手机”:“sdfsdfdsfsd ”, “城市”: “sdfsf”, “电子邮件”: “13123123”}]

I want to inset this json array into my mysql database so i have used this code its not working please tell me how to make it done. My php code.

我想将这个json数组插入到我的mysql数据库中,所以我使用了这个代码它不工作请告诉我如何完成它。我的PHP代码。

<?php 

  $json = file_get_contents('php://input');
  $obj = json_decode($data,true);
  $obj[0]['name'];
  $obj[1]['phone'];
  $obj[2]['city'];
  $obj[3]['email'];

 //Database Connection
  require_once 'db.php';

 /* insert data into DB */
      mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email) 
      VALUES ('".$obj->{'name'}."', '".$obj->{'phone'}."', '".$obj->{'city'}."', '".$obj->{'email'}."')");

 //database connection close
   mysql_close($con);

   //}
  ?>

This above code only have used to store the values into my database please help me out to resolve this problem.

以上代码只用于将值存储到我的数据库中,请帮我解决这个问题。

Thanks.

谢谢。

2 个解决方案

#1


1  

Within your json_decode function you've specified 'true', this will make return an array.

在你的json_decode函数中你指定了'true',这将返回一个数组。

You're then trying to access this information as an object, which won't work. Plus the fact that you are given two arrays back would suggest that you're going to need a foreach statement to insert both pieces of data in to the database.

然后,您尝试将此信息作为对象访问,这将无法正常工作。此外,您将获得两个数组,这表明您需要使用foreach语句将两个数据插入数据库。

Have another look in to foreach statements and arrays, there's plenty of information around to give you a hand.

再看一下foreach语句和数组,有很多信息可以帮助你。

For example:

例如:

$array = json_decode($data,true);

foreach($array as $item) {
    mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email) 
  VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

}

To add on to this, you're using a deprecated mysql_* function, you should look in to using mysqli or PDO for increased security.

要添加到此,您使用的是不推荐使用的mysql_ *函数,您应该使用mysqli或PDO来提高安全性。

#2


0  

This is another easy way

这是另一种简单的方法

<?php
    $json_value = file_get_contents('your_json_data_link');
    $array = json_decode($json_value,true);
    require_once 'db.php';
    foreach($array as $item) {
        $insert_value = "INSERT INTO `db512172115`.`trial` (name, phone, city,email)VALUES 
        ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

        if ($con->query($insert_value ) === TRUE) {
            echo "Record Successfully<br>";
        }
        else
        {
            echo "Error: " . $insert_value . "<br>" . $con->error;
        }
    }
?>

#1


1  

Within your json_decode function you've specified 'true', this will make return an array.

在你的json_decode函数中你指定了'true',这将返回一个数组。

You're then trying to access this information as an object, which won't work. Plus the fact that you are given two arrays back would suggest that you're going to need a foreach statement to insert both pieces of data in to the database.

然后,您尝试将此信息作为对象访问,这将无法正常工作。此外,您将获得两个数组,这表明您需要使用foreach语句将两个数据插入数据库。

Have another look in to foreach statements and arrays, there's plenty of information around to give you a hand.

再看一下foreach语句和数组,有很多信息可以帮助你。

For example:

例如:

$array = json_decode($data,true);

foreach($array as $item) {
    mysql_query("INSERT INTO `db512172115`.`trial` (name, phone, city, email) 
  VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

}

To add on to this, you're using a deprecated mysql_* function, you should look in to using mysqli or PDO for increased security.

要添加到此,您使用的是不推荐使用的mysql_ *函数,您应该使用mysqli或PDO来提高安全性。

#2


0  

This is another easy way

这是另一种简单的方法

<?php
    $json_value = file_get_contents('your_json_data_link');
    $array = json_decode($json_value,true);
    require_once 'db.php';
    foreach($array as $item) {
        $insert_value = "INSERT INTO `db512172115`.`trial` (name, phone, city,email)VALUES 
        ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");

        if ($con->query($insert_value ) === TRUE) {
            echo "Record Successfully<br>";
        }
        else
        {
            echo "Error: " . $insert_value . "<br>" . $con->error;
        }
    }
?>