我在文本文件中有一些 JSON 格式的数据,如下所示。我需要使用 php 将此数据插入 mysql 但不能这样做。
{"address":"+92 334 6629424","service_center":"Test Sending Sms","id":3,"locked":0,"person":0,"protocol":0,"read":0,"reply_path_present":2,"seen":0,"error_code":0,"status":1,"date":1873326412,"thread_id":1,"type":-1}
我的 PHP 文件有这样的代码。
<?php $source_file = "SMS2012-05-21.txt"; $handle = fopen("SMS2012-05-21.txt", "r");
$col_names = implode(",", fgetcsv($handle)); // Getting comma separated list of col name
$link = mysql_connect('localhost', 'root', '');
mysql_select_db("messages");
while (($data = fgetcsv($handle)) !== FALSE) {
$values = "";
foreach($data as $key => $value) {
if ($key != 0) $values .= ", ";
$values .= "'".mysql_escape_string($value)."'";
}
mysql_query('INSERT INTO messages ('.$col_names.') VALUES ('.$values.')');
}
?>
我找不到任何结果或任何错误。任何人都可以在这方面帮助我,我错了?