PHP读取txt json 遍历值批量导入mysql数据库

180it 2022-07-20 AM 999℃ 0条
include_once "config.php";
include_once "db.php";
include_once "function.php";


$txt_array = glob("*.{txt,dat}",GLOB_BRACE); 
$txt = array_rand($txt_array); 
$file= $txt_array[0];

$strdate= explode("_",$file)[0];
//echo(strtotime($strdate));
//echo(date('Y-m-d', strtotime($strdate)));
//exit;



$res = file_get_contents($file);
//echo $res;
$josn=json_decode($res,true);
$item= $josn['item'];
//echo $item[1]["num_iid"];
//exit;

$classid=1;


foreach ($item as $v)
{
// echo $v["num_iid"].'<br/>'; 
$num_iid=$v["num_iid"];
$price=$v["price"];
$pic_url=$v["pic_url"];
$title=$v["title"];
$sales=$v["sales"];
$detail_url=$v["detail_url"];
$cdate=date('Y-m-d', strtotime($strdate));


    $sql = "INSERT INTO info(classid,num_iid,price,pic_url,title,sales,detail_url,cdate) VALUES (:classid,:num_iid,:price,:pic_url,:title,:sales,:detail_url,:cdate)";  
    $arr = array(
    ':classid'=>$classid,
    ':detail_url'=>$detail_url,
    ':sales'=>$sales,
    ':title'=>$title,
    ':pic_url'=>$pic_url,
    ':price'=>$price,
    ':cdate'=>$cdate,
    ':num_iid'=>$num_iid
    //':time'=>date("Y-m-d H:i:s",time())
    );
    

        $result = setsql("insert",$sql,$arr);
         echo $result;
          if($result)
          {
              echo $val."添加成功<BR/>".PHP_EOL;
          }else
          {
              echo $val."添加失败<BR/>".PHP_EOL;
          }
          
      
          
}

sleep(3);
$status=unlink($file);   //删除文件 
if($status){  
echo $file." File deleted successfully";    
}else{  
echo $file." Sorry!";    
}       
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP读取txt json 遍历值批量导入mysql数据库