小编Jes*_*ica的帖子

用PHP将数组插入MySQL数据库?

这是我目前的代码:

// connect to database  
$con = mysqli_connect("localhost","username","password","database");
if (!$con) {
    die('Could not connect: ' . mysqli_error());
}

// get the json file
$jsondata = file_get_contents('http://www.example.com');

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

// assign each of the variables
$id = $data['0']['id'];
$name = $data['0']['name'];
$status = $data['0']['status'];

$insertstatement = mysqli_query($con,"INSERT INTO `table` (`id`, `name`, `status`) VALUES ('".$id."', '".$name."', '".$status."');");
Run Code Online (Sandbox Code Playgroud)

从技术上讲,这段代码正在运行,但它只是添加了第一项.

例如,这是导入的原始json:

[
   {
      "id":"19839",
      "status":"active",
      "name":"First Name",
   },
   {
      "id":"19840",
      "status":"active",
      "name":"Second Name",
   },
   {
      "id":"19841", …
Run Code Online (Sandbox Code Playgroud)

php mysql mysqli

1
推荐指数
1
解决办法
6444
查看次数

标签 统计

mysql ×1

mysqli ×1

php ×1