Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将文本文件中的数据插入mysql?_Php_Json - Fatal编程技术网

Php 如何将文本文件中的数据插入mysql?

Php 如何将文本文件中的数据插入mysql?,php,json,Php,Json,我在文本文件中有一些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,"thre

我在文本文件中有一些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.')');
 }
 ?>

也许我遗漏了什么,你应该这样使用它:

<?php $source_file  = "SMS2012-05-21.txt"; 
$handle = fopen("SMS2012-05-21.txt", "r");
$data = fread($handle, filesize($source_file));
$jsonArray = json_decode($data, true);

$keys = implode(',', array_keys($jsonArray));
$values = "'" . implode("','", $jsonArray) . "'";

$link = mysql_connect('localhost', 'root', '');
mysql_select_db("messages");

mysql_query('INSERT INTO messages ('.$keys.') VALUES ('.$values.')');
您应该使用函数来操作json数据

<?php


  $source_file  = "SMS2012-05-21.txt";
  $string = file_get_contents($source_file);
  $json = json_decode($string,true);

  //DB Conn Handling Stuff

  $cols = array(); $values = array();

  foreach($json as $key=>$value)
  {
      array_push($cols,'`' . $key . '`');
      if(is_string($value))
      {
         array_push($values,'\''.$value.'\'');
      }
      else
      {
         array_push ($values, $value);
      }
  }

  $col_name = implode(',',$cols);
  $col_value = implode(',',$values);

  $query = 'INSERT INTO messages('.$col_name.') VALUES ('.$col_value.')';
  mysql_query($query,$connection) or die(echo mysql_error());      

?>


您应该在文本文件上使用
json\u decode
,因为它是json数据,而不是CSV数据。我收到以下错误作为对该代码的响应。警告:array_push()希望参数1是数组,字符串在第16行的D:\wamp\www\admin\admin\jsondecode2.php中给出,注意:在第21行的D:\wamp\www\admin\admin\jsondecode2.php中数组到字符串的转换。请告诉我应该在哪里更正。对不起,我在数组\u push上犯了一个错误,按参数的顺序排列。首先是数组,然后是要推送的字符串。我已经编辑了这篇文章,现在已经更正了,它现在给出了以下错误。您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,了解使用“读取、回复、路径、当前、看到、错误、代码、状态、日期、线程id、类型”值附近的正确语法(92 3'在第1strange行…我没有测试数据库插入,但是,查询的回音对我来说似乎是正确的…请执行此操作并在此处发布结果:
echo$query;
可能是报价问题我编辑了函数,请替换并发布反馈我在该代码的响应中收到以下错误:警告:json\u decode()预期参数1为字符串,资源在第4行的D:\wamp\www\admin\admin\jsondecode2.php中给出,警告:array\u keys()预期参数1为数组,在第6行的D:\wamp\www\admin\admin\jsondecode2.php中给出null,警告:内爆()[函数.内爆]:在第6行的D:\wamp\www\admin\admin\jsondecode2.php中传递的参数无效。请尽快告诉我它的解决方案。它没有给出任何结果。我以前创建了与$keys相同列名的messages表。但此代码没有给出任何错误或结果。