数据库连接但不插入记录?

时间:2010-09-06 23:50:59

标签: php mysql

我整理了一个简单的脚本,从tweetmeme api中提取内容,然后将其插入数据库以进一步格式化。这是我目前拥有的,但它没有插入记录,也没有为数据库连接返回任何错误,所以我可以假设它连接并在插入时失败。

   $dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '****';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'footy';
mysql_select_db($dbname) or die('Couldnt connect to database table');



  $tweetmeme = "http://api.tweetmeme.com/stories/popular.xml?category=sports-soccer&count=30" ; 

  $xml = @simplexml_load_file($tweetmeme) or die ("no file loaded") ; 

 echo count($xml->stories->story)."stories in the XML file<br /><br />";
echo $xml->getName() . "<br /><br />";
foreach($xml->stories->story as $story)
{
    $title=$story->title;
    $url=$story->url;
    $media_type=$story->media_type;
    $created=$story->created_at;
    //$current_time=$date();
    $url_count=$story->url_count;
    $comment_count=$story->comment_count;
    $excerpt=$story->excerpt;

    $sql = "INSERT INTO ft_tweets (title,url,media_type,created_at,mention_count,comment_count,excerpt) VALUES ($title,$url,$media_type,$created,$url_count,$comment_count,$excerpt)";
    $result = mysql_query($sql);

对各个项执行vardump()会返回“object(SimpleXMLElement)”,这就是示例link text

的xml结构

1 个答案:

答案 0 :(得分:0)

确保您的查询成功,请尝试添加or mysql_error(),以查看是否有任何错误:

$result = mysql_query($sql) or die(mysql_error());