2013-04-10 46 views
2

我真的很想有人花一點時間看看我的代碼。我正在解析一些新聞內容,並且可以將最初的解析插入到包含新聞網址和標題的數據庫中。我想進一步擴展它,傳遞每篇文章鏈接並解析文章的內容並將其包含在我的數據庫中。初步分析工作完全是這樣的:如何將任意數量的值綁定到mysqli中的預處理語句?

<?php 
include_once ('connect_to_mysql.php'); 
include_once ('simple_html_dom.php'); 
$html = file_get_html('http://basket-planet.com/ru/'); 
$main = $html->find('div[class=mainBlock]', 0); 
    $items = array(); 
    foreach ($main->find('a') as $m){ 
    $items[] = '("'.mysql_real_escape_string($m->plaintext).'", 
       "'.mysql_real_escape_string($m->href).'")'; 
    } 
$reverse = array_reverse($items); 
mysql_query ("INSERT IGNORE INTO basket_news (article, link) VALUES 
      ".(implode(',', $reverse)).""); 
?> 

正如你所看到的,我使用的是PHP Simple HTML DOM Parser.擴大,我嘗試使用mysqli的說法,我可以綁定參數,這樣所有的HTML標籤插入到了我的數據庫。我之前用XML解析完成了這個。問題是我不知道如何將數組綁定,看看我的代碼是否正確,是否會以這種方式工作......這裏是整個代碼:

<?php 
$mysqli = new mysqli("localhost", "root", "", "test"); 
$mysqli->query("SET NAMES 'utf8'"); 
include_once ('simple_html_dom.php'); 
$html = file_get_html('http://basket-planet.com/ru/'); 
//find main news 
$main = $html->find('div[class=mainBlock]', 0); 
$items = array(); 
    foreach ($main->find('a') as $m){ 
    $h = file_get_html('http://www.basket-planet.com'.$m->href.''); 
    $article = $h->find('div[class=newsItem]'); 
    //convert to string to be able to modify content 
    $a = str_get_html(implode("\n", (array)$article)); 
     if(isset($a->find('img'))){ 
     foreach ($a->find('img') as $img){ 
      $img->outertext = '';}} //get rid of images 
     if(isset($a->find('a'))){ 
     foreach ($a->find('a') as $link){ 
      $link->href = 'javascript:;'; 
      $link->target = '';}} //get rid of any javascript 
     if(isset($a->find('iframe'))){ 
     foreach ($a->find ('iframe') as $frame){ 
      $frame->outertext = '';}} //get rid of iframes 
    @$a->find('object', 0)->outertext = ''; 
    @$a->find('object', 1)->outertext = ''; 
    //modify some more to retrieve only text content 
    //put entire content into a div (will if statements work here???) 
    $text_content = '<div>'.$a.'<br>'. 
     ($a->find('object', 0)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 0)->data.'">Play Video</a>&nbsp;&nbsp;') 
     ($a->find('object', 1)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 1)->data.'">Play Video</a>&nbsp;&nbsp;') 
     ($a->find('iframe[src*=youtube]', 0)->src > 0 ? '<a target="_blank" href="'.$a->find('iframe', 0)->src.'">Play Video</a>&nbsp;&nbsp;') 
     //couple more checks to see if video links are present 
    .'</div>'; 
$items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")'; 
} 
//reverse the array so the latest items have the last id 
$reverse = array_reverse($items); 
$stmt = $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)"); 
$stmt->bind_param ???; //(implode(',', $reverse)); 
$stmt->execute(); 
$stmt->close(); 
?> 

因此,邏輯是對的每一個HREF一篇文章發現,我通過它來解析內容,我試圖將它添加到數組中。我可能有很多錯誤,但是我還不能測試它,因爲我不知道如何綁定它來查看它是否有效。而且我也不確定是否可以在$ text_content div內執行if語句...意思是顯示「播放視頻」(如果存在)。所以請,如果有人可以花時間與我一起工作,我會很感激。

UPDATE:將if語句更改爲$ text_content div中的比較運算符。

+0

格式太糟糕了..我沒有看到'foreach($ main'已關閉..請查看[PSR-2-coding-style](https://github.com/php-圖/無花果標準/ blob /主/接受/ PSR-2-coding-style-guide.md) – bitWorking 2013-04-10 18:15:09

+0

我關閉了它...你可以請看看代碼?foreach關閉後,一切都被輸入到$ items數組 – user2025469 2013-04-10 18:18:51

回答

4

這正是mysqli真的很尷尬的場景。要綁定多個參數,必須將它們全部作爲可變長度參數列表傳遞給mysql-> bind_param(),但棘手的部分是您必須通過引用將它們綁定到。 PHP中的引用可能相當混亂。

下面是一個粗略的例子(雖然我沒有測試過這個確切的代碼):

$stmt = $mysqli->prepare("INSERT IGNORE INTO test_news 
    (article, link, text_cont) VALUES (?,?,?)"); 
foreach ($reverse as &$value) { 
    $params[] = &$value; 
} 
array_unshift(str_repeat('s', count($params))); 
call_user_func_array(array($stmt, 'bind_param'), $params); 

我覺得它更容易使用PDO的時候,我想寫一個通用的函數參數綁定到SQL。不需要綁定,只需將一個數組值傳遞給PDOStatement :: execute()方法即可。

$stmt = $pdo->prepare("INSERT IGNORE INTO test_news 
    (article, link, text_cont) VALUES (?,?,?)"); 
$stmt->execute($reverse); 

更新:如果您需要$項目包含多行數據,我會做這種方式:

首先,建立$項目時,讓數組的數組,

:不是串聯的值加在一起:

foreach ($main->find('a') as $m){ 
    $items[] = array($m->plaintext, $m->href, $text_content); 
} 

然後準備爲每個元組執行事先準備好的聲明,一旦一個INSERT語句插入一行,並遍歷$項目

$stmt = $pdo->prepare("INSERT INTO test_news 
    (article, link, text_cont) VALUES (?,?,?)"); 
foreach ($items as $tuple) { 
    $stmt->execute($tuple); 
} 

我不知道爲什麼你在所有使用array_reverse(),我不知道爲什麼你正在使用INSERT忽略,所以我離開了那些的。

+0

嘿比爾,謝謝你回到我身邊。現在我正在修復我的代碼中的錯誤。我擺脫了所有的查詢,只是輸出內容,只是爲了確保代碼的運行。我想我的工作,或多或少。所以一旦我把事情做好,我會立即嘗試你的建議。對於mysqli,特別是PDO,我很新,正如你所知道的。 PDO將數組分成三部分,並執行你寫的方式?那容易? – user2025469 2013-04-10 18:41:14

+0

會這樣工作: $ stmt-> bind_param('sss',$ reverse [0],$ reverse [1],$ reverse [2]); – user2025469 2013-04-10 18:43:37

+1

這裏沒有提到的是'VALUES'具有像'(?,?,?),(?,?,?),(?,?,?)'等可變長度,所以上面的工作很容易 – bitWorking 2013-04-10 18:45:43

相關問題