3

誰かに少し時間をかけて私のコードを見てもらいたいです。いくつかのニュース コンテンツを解析しています。ニュースの URL とタイトルを含む最初の解析をデータベースに挿入できます。さらに拡張して、各記事のリンクを渡し、記事の内容を解析してデータベースに含めたいと考えています。最初の解析は次のように完全に機能します。

<?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 を使用しています。 拡張するには、すべての html タグがデータベースに挿入されるようにパラメーターをバインドできる mysqli ステートメントを使用しようとしています。以前に 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 ステートメントを実行できるかどうかもわかりません...存在する場合は「ビデオの再生」を表示することを意味します。誰かが私と一緒にこれに取り組むために時間を割いてくれるなら、私は本当に感謝しています。

更新: $text_content div の if ステートメントを比較演算子に変更しました。

4

1 に答える 1

5

これはまさに 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);

パラメーターを SQL にバインドする汎用関数を作成する場合は、PDO を使用する方がはるかに簡単です。バインドは必要ありません。値の配列を PDOStatement::execute() メソッドに渡すだけです。

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

更新: $items に複数行のデータを含める必要がある場合は、次のようにします。

まず、$items を作成するときは、値を連結するのではなく、配列の配列にします。

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

次に、1 行を挿入する INSERT ステートメントを準備し、タプルごとに 1 回、準備されたステートメントを実行する $items をループします。

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

なぜあなたが array_reverse() を使用していたのかまったくわかりません。また、なぜ INSERT IGNORE を使用していたのかもわからないので、それらを省略しました。

于 2013-04-10T18:33:57.153 に答える