如何将以下格式保存到db表中

时间:2017-01-24 08:47:24

标签: php wordpress

我有这种数组格式,有人说如何将这些值保存到数据库中

[{"Name":"Honolulu","Address":"Honolulu, HI, USA","Phone":null,"Rating":null,"Website":null},{"Name"
:"Aston at the Executive Centre Hotel","Address":"1088 Bishop St, Honolulu, HI 96813, USA","Phone":"
(808) 539-3000","Rating":3.8,"Website":"http:\/\/www.astonexecutivecentre.com\/"},{"Name":"The Queen's
 Medical Center","Address":"1301 Punchbowl St, Honolulu, HI 96813, USA","Phone":"(808) 538-9011","Rating"
:4,"Website":"http:\/\/www.queensmedicalcenter.net\/"},{"Name":"Castle Resorts & Hotels Inc","Address"
:"500 Ala Moana Blvd, Honolulu, HI 96813, USA","Phone":"(808) 545-3510","Rating":null,"Website":"http
:\/\/www.castleresorts.com\/"}]

1 个答案:

答案 0 :(得分:0)

这是一个标准的mysql插入语句。

INSERT INTO TABLE(COLUMN1, COLUMN2, ....) VALUES (VALUE1, VALUE2..)

我假设你有一个名为 testdb 的数据库,它包含名为 mytable 的表

你可以使用最好的准备语句,但既然你看起来像初学者,你可以试试下面的例子

<?php

/* Your long json string */    
$json_string='[{"Name":"Honolulu","Address":"Honolulu, HI, USA","Phone":null,"Rating":null,"Website":null},{"Name":"Aston at the Executive Centre Hotel","Address":"1088 Bishop St, Honolulu, HI 96813, USA","Phone":"    (808) 539-3000","Rating":3.8,"Website":"http:\/\/www.astonexecutivecentre.com\/"},{"Name":"The Queen\'s     Medical Center","Address":"1301 Punchbowl St, Honolulu, HI 96813, USA","Phone":"(808) 538-9011","Rating":4,"Website":"http:\/\/www.queensmedicalcenter.net\/"},{"Name":"Castle Resorts & Hotels Inc","Address":"500 Ala Moana Blvd, Honolulu, HI 96813, USA","Phone":"(808) 545-3510","Rating":null,"Website":"http    :\/\/www.castleresorts.com\/"}]';

/*Convert into php array from json string */
$data = json_decode($json_string,true);


/* You need to connect first */
$con=  mysqli_connect("localhost", "root","password","testdb");

if (mysqli_connect_errno()) {
    die( "Failed to connect to MySQL: " . mysqli_connect_error() );
}

/* Loop through array */
foreach($data as $row )
{

    /* columns which you have */
    $columns = "`".implode("`,`",array_keys($row))."`";

    /* Escape values */
    $escaped_values = array_map(array($con,'real_escape_string'), array_values($row));

    /* create a string separated by comma */
    $values  = "'".implode("','", $escaped_values)."'";

    /* Finally build insert string */ 
    $sql = "INSERT INTO `mytable`($columns) VALUES ($values)";

    /* Execute query - here we are inserting row data to db table */
    if(! mysqli_query($con,$sql) ) 
    {
       /* error if any */
       die(mysqli_error($con));
    }

}

?>

上面会在每次迭代中产生如下所示的插入语句

INSERT INTO `mytable`(`Name`,`Address`,`Phone`,`Rating`,`Website`) VALUES ('Honolulu','Honolulu, HI, USA','','','')
INSERT INTO `mytable`(`Name`,`Address`,`Phone`,`Rating`,`Website`) VALUES ('Aston at the Executive Centre Hotel','1088 Bishop St, Honolulu, HI 96813, USA','    (808) 539-3000','3.8','http://www.astonexecutivecentre.com/')
INSERT INTO `mytable`(`Name`,`Address`,`Phone`,`Rating`,`Website`) VALUES ('The Queen\'s     Medical Center','1301 Punchbowl St, Honolulu, HI 96813, USA','(808) 538-9011','4','http://www.queensmedicalcenter.net/')
INSERT INTO `mytable`(`Name`,`Address`,`Phone`,`Rating`,`Website`) VALUES ('Castle Resorts & Hotels Inc','500 Ala Moana Blvd, Honolulu, HI 96813, USA','(808) 545-3510','','http    ://www.castleresorts.com/')

对于您的json字符串

[{
    "Name": "Honolulu",
    "Address": "Honolulu, HI, USA",
    "Phone": null,
    "Rating": null,
    "Website": null
}, {
    "Name": "Aston at the Executive Centre Hotel",
    "Address": "1088 Bishop St, Honolulu, HI 96813, USA",
    "Phone": "    (808) 539-3000",
    "Rating": 3.8,
    "Website": "http:\/\/www.astonexecutivecentre.com\/"
}, {
    "Name": "The Queen\'s     Medical Center",
    "Address": "1301 Punchbowl St, Honolulu, HI 96813, USA",
    "Phone": "(808) 538-9011",
    "Rating": 4,
    "Website": "http:\/\/www.queensmedicalcenter.net\/"
}, {
    "Name": "Castle Resorts & Hotels Inc",
    "Address": "500 Ala Moana Blvd, Honolulu, HI 96813, USA",
    "Phone": "(808) 545-3510",
    "Rating": null,
    "Website": "http    :\/\/www.castleresorts.com\/"
}]