PHP 利用simplexml_load_file 读取XML对应的字段 然后存入mysql数据库



    
        标题1
        /news/2015/news-12-23_2.jpg
        
            
                /news/2015/news-12-23.jpg
            
            
                /news/2015/news-12-23_2.jpg
            
            
                /news/2015/news-12-23_3.jpg
            
            
                /news/2015/news-12-23_1.jpg
            
            
                /news/2015/news-12-23_4.jpg
            
        

    
    
        标题1
        /news/2015/1.png
        
            
                /news/2015/1.1.jpg
            
            
                /news/2015/1.2.png
            
            
                /news/2015/1.3.jpg
            
            
                /news/2015/1.4.jpg
            
            
                /news/2015/1.5.jpg
            
            
                /news/2015/1.6.jpg
            
            
                /news/2015/1.7.jpg
            
            
                /news/2015/1.8.png
            

        
    


总体的思路是利用print_r 进行打印调试,遇到object就用->读取,遇到Array就用[]读取,在调试的时候遇到中文编码的问题用header("Content-Type: text/html; charset=UTF-8");
由于个人水平有限,调试花了一个多小时,希望看到这Blog的人半个小时调试出来。

title;

   $thumbnail = (string)$news->thumbnail;
   $content = (string)$news->content;

//看到这段代码的同学也可以研究一下VALUES中的变量$title,$thumbnail,$content必须要加‘’,如果没有加的话就无法insert   

mysql_query("INSERT INTO news (newsid, year,title,thumbnail,content) VALUES ($id, $year,'$title','$thumbnail','$content')")or die(mysql_error());

   foreach ($news->images->image as $image) {
        $image = (string)$image->source;
        mysql_query("INSERT INTO sub_news (news_id, image) VALUES ($id, '$image')")or die(mysql_error());
   }
}
mysql_close($con);
?>

你可能感兴趣的:(PHP 利用simplexml_load_file 读取XML对应的字段 然后存入mysql数据库)