0

所以我想做的是:运行这个查询:SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet =1它输出两个结果:图像1

我想将这两个结果附加到$resultvar 并通过结果(为 2 - 如图所示)作为$r,然后输出的部分$r因此为

# Source of Article Info-->
            $SrcTitle=$newsStory[$i]->title;
            $SrcLink=$newsStory[$i]->link;

    # Actual News Article Info -->
            $title=$newsStory[$i]->title;
            $desc=$newsStory[$i]->description;

完整代码如下所示,目前仅从sports.yahoo.com/tennis/rss.xml的第二个结果中提取:

## Loop through results from mysql
try{
    #connection string
        // $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=mydatabase',array(PDO::ATTR_PERSISTENT => true));
        $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=mydatabase','myuser','mypass',array(PDO::ATTR_PERSISTENT => true));
        $q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=1");
    #call stored proc
        $q->execute();
    #get the rows into an array
        $result = $q->fetchAll();
        $newsStory[] = array();
        foreach($result as $r){
            $xmlUrl = $r['FW_ArtSrcLink'];
            $ConvertToXml = simplexml_load_file($xmlUrl);
            # -> Setup XML
            $newsStory = $ConvertToXml->channel->item;
        }

    # -----> Load News Stories
        for($i = 0;$i<sizeof($newsStory); $i++){

    # Source of Article Info-->
            $SrcTitle=$newsStory[$i]->title;
            $SrcLink=$newsStory[$i]->link;

    # Actual News Article Info -->
            $title=$newsStory[$i]->title;
            $desc=$newsStory[$i]->description;

    # Output Results ------------>      
            echo '<hr>';
            echo '<strong>'.'Title:'.$title.'</strong>'.'(via: <a href=\''.$SrcLink.'\'>'.$SrcTitle.'</a>'.'<br />';
            //echo 'Link:'.$link.'<br />';
            echo 'Description'.$desc.'<br>';
            ##echo 'count '.count($result);
            echo '<hr>';
        }
} // try

我在这里做错了什么?

来自单个 db 结果的简单输出示例,我将其称为硬编码结果而不是动态使用

$xmlUrl ="http://edge1.catalog.video.msn.com/videoByTag.aspx?tag=Fox%20Sports_NBA%20news&ns=MSNVideo_Top_Cat&mk=us&sd=-1&sf=ActiveStartDate&vs=0&ind=&ps=&rct=&ff=88&responseEncoding=rss&title=FOX%20Sports%20Video%20on%20MSN:%20NBA&template=foxsports&p=foxsports";
$ConvertToXml = simplexml_load_file($xmlUrl);

img2

4

1 回答 1

1

尝试更改此代码块:

    $newsStory = array();
    foreach($result as $r){
        $xmlUrl = $r['FW_ArtSrcLink'];
        $ConvertToXml = simplexml_load_file($xmlUrl);
        # -> Setup XML
        $newsStory[] = $ConvertToXml->channel->item;
    }

您之前的代码将 $newsStory 声明为数组数组。然后它覆盖了这个定义并将 $newsStory 变成了一个简单的字符串。
它解释了您注意到的行为:“目前仅从第二个结果中提取”。

而且由于您没有告诉我们任何 PHP 错误,您可能没有打开它们。在脚本的开头添加这些行:

error_reporting(E_ALL); // tells PHP to report all errors
ini_set('display_errors', 1); // tells PHP to display all errors on the screen

文档:数组 ini_set error_reporting

于 2012-07-26T00:49:00.973 回答