【问题标题】:How to saved this below format into db table如何将以下格式保存到数据库表中
【发布时间】:2017-06-08 23:37:23
【问题描述】:

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

[{"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\/"}]

【问题讨论】:

  • 到目前为止,您自己尝试过什么
  • 我使用了这个代码 $col_count=1;全球 $wpdb; $tablename=$wpdb->prefix.'wp_map_contacts'; $data =array('id' => $col_count++, 'Name' => $name, 'Address' => $formatted_addr, 'Phone' => $formatted_phone, 'Rating' => $rating, '网站' => $网站); $success = $wpdb->insert($tablename, $data); if($success){ echo "插入成功"; } else { echo "发生错误"; }
  • 这是一个 JSON 数据,默认情况下始终为字符串格式,您可以使用 dataType TEXT 存储到数据库中。很容易。
  • 你能给我发个例子吗?

标签: php wordpress


【解决方案1】:

这是一个标准的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));
    }

}

?>

Above 会在每次迭代中产生如下插入语句

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\/"
}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-18
    • 2022-06-10
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2015-07-08
    相关资源
    最近更新 更多