1

私は php html dom パーサーを使用して 2 つのサイトから情報を収集しています。エコーされると、希望どおりに動作しますが、変数の一部をデータベースに保存しようとすると、最後の結果のみが保存されます。

これが私が取り組んできたコードです。

<?php
include_once('simple_html_dom.php');

ini_set('display_errors', true);
error_reporting(E_ALL);



$host="localhost";
$username="root";
$password="";
$database="betatv_local";

mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");


$html = file_get_html('http://www.sidereel.com/Supernatural');

// find the title and echo it out
foreach($html->find('.title') as $title)
foreach($title->find('.sr-header') as $title2)
echo $title2->plaintext .'<br>';

echo '<br>';

// find the desc and echo it out
foreach($html->find('.summary') as $desc)
foreach($desc->find('.full-summary') as $descc)
echo $descc->plaintext .'<br>';


echo '<br>';

// find the showinfo and echo it out
foreach($html->find('.count') as $info)
echo $info->plaintext;



// find the airtime and echo it out
foreach($html->find('.airtime') as $air)
echo $air->plaintext;



// find the show status and echo it out
foreach($html->find('.status') as $status)
echo $status->plaintext .'<br>';

echo '<br>';

// find the show img and echo it out
foreach($html->find('.show-image') as $cover_img)
foreach($cover_img->find('img') as $cover_img_link)
echo $cover_img_link->src .'<br>';

echo '<br>';





$html = file_get_html('http://www.1channel.ch/watch-11054-Supernatural');

    // find the show episodes and echo it out
foreach($html->find('.tv_container') as $episodes)
foreach($episodes->find('a') as $episode_links)

echo $episode_links->plaintext .'<br>';


$query = "INSERT INTO tv_shows (show_name,show_info,show_airs,show_status,show_cover,show_desc,show_episodes) VALUES('$title2->plaintext','$info->plaintext','$air->plaintext','$status->plaintext','$cover_img_link->src','$descc->plaintext','$episode_links->plaintext')";

mysql_query($query) or die(mysql_error());

// clean up memory
    $html->clear();
    unset($html);

?>

エコーするとこんな感じ。

スーパーナチュラル

ジェンセン・アクレスとジャレッド・パダレッキが、悪魔の手による母親の死と父親の謎の失踪の背後にある意味を探すディーンとサム・ウィンチェスターとして主演します。

8 シーズン、163 エピソード | 放映: The CW で午後 9:00 | ショーのステータス: 休止

http://s3.sidereel.com/tv_shows/4373/large/sc_supernatural.jpg

パート 2 シーズン 3 エピソード 0 - プロモ エピソード 1 - マグニフィセント セブン エピソード 2 - 子供たちは大丈夫 エピソード 3 - ブラック ロックの悪い日 エピソード 4 - シン シティ エピソード 5 - ベッドタイム ストーリー エピソード 6 - 朝の赤い空 エピソード 7 - フレッシュBlood Episode 8 - A Very Supernatural Christmas Episode 9 - Malleus Maleficarum Episode 10 - Dream A Little Dream Of Me Episode 11 - Mystery Spot Episode 12 - Jus In Bello Episode 13 - Ghostfacers Episode 14 - Long-Distance Call Episode 15 - Time Is On My Side Episode 16 - No Rest for the Wicked Season 4 Episode 0 - Promo Episode 1 - Lazarus Rising Episode 2 - Are You There God? It's Me, Dean Winchester Episode 3 - In the Beginning Episode 4 - Metamorphosis Episode 5 - Monster Movie Episode 6 - Yellow Fever Episode 7 - It's the Great Pumpkin, フィル エピソード 6 - スラッシュ フィクション エピソード 7 - メンタリスト エピソード 8 - 結婚式の時間! エピソード 9 - 友達を獲得し、モンスターに影響を与える方法 エピソード 10 - 死の扉 エピソード 11 - ベビーシッターの冒険 エピソード 12 - タイム アフター タイム エピソード 13 - スライス ガールズ エピソード 14 - プラッキー ペニーホイッスルの魔法の動物園 エピソード 15 - レポマン エピソード 16 - アウト ウィズ古い エピソード 17 - 生まれ変わったアイデンティティ エピソード 18 エピソード 19 エピソード 20 - ダンジョンズ アンド ドラゴンズ タトゥーの少女 エピソード 21 - 読書は基本的です エピソード 22 - 血が流れます エピソード 23 - 適者生存 シーズン 8 エピソード 0 -シーズン 8 プロモ エピソード 1 - ケビンについて話す必要がある エピソード 2 - What's Up, タイガーママ?エピソード 3 - 心痛 エピソード 4 - 噛まれたエピソード 5 - ブラッド ブラザー エピソード 6 - サザン コンフォート エピソード 7 - ケビンの小さなスライス エピソード 8 - ハンテリ ヒロイチ エピソード 9 - 市民の牙 エピソード 10 - 引き裂かれたエピソード 11 - LARP と本物の少女Episode 12 - As Time Goes By Episode 19 - 重大な重要性の

私の変数 $episode_links を除いて、すべてが正常にデータベースに保存されます。節約するだけです

時が経つにつれて 第19話 - 重大な重要性について

最後の結果だけでなく、すべての情報を変数に保存する必要があります。ここで何が間違っているのですか?

4

2 に答える 2

0

クエリを実行する前に、 mysql_real_escape_stringを使用してみてください

于 2013-02-05T23:19:17.903 に答える
0

phpforeachコンストラクトは、配列内の各項目に対して次のステートメントを実行します。foreach で複数の処理 (エコーやデータベースへの保存など) を実行したい場合は、実行したいすべての処理を中括弧で囲む必要があります{}

$html = file_get_html('http://www.1channel.ch/watch-11054-Supernatural');
$ep_links = "";

// find the show episodes and echo it out
foreach($html->find('.tv_container') as $episodes) {
    foreach($episodes->find('a') as $episode_links) {

        echo $episode_links->plaintext .'<br>';
        $ep_links .= $episode_links->plaintext;
    }
}

$ep_links = mysql_real_escape_string($ep_links);
$query = "INSERT INTO tv_shows (show_name,show_info,show_airs,show_status,show_cover,show_desc,show_episodes) VALUES('$title2->plaintext','$info->plaintext','$air->plaintext','$status->plaintext','$cover_img_link->src','$descc->plaintext','$ep_links')";

mysql_query($query) or die(mysql_error());
// clean up memory
$html->clear();
unset($html);

解析中に各エピソードを変数に追加し、それをデータベースに挿入するように編集しました。

于 2013-02-05T22:44:49.720 に答える