1

データベース ( SQL|PhpMyAdmin ) にデータを返す HTML テーブルをスクレイピングするスクリプトを以下に示します。(XAMPP)を使用してブラウザでスクリプトを実行します

スクレーパーは正常に動作し、ブラウザーにデータを表示したり、Excel ファイルとしてダウンロードしたりできます。[ $conn = mysql_connect ]使用して接続文字列を SQL に追加しました。

質問:

  • mysqli_connect を使用すると、データベースへの接続に問題は発生しませんが、返されたデータにいくつかの行がありません。たとえば、テーブルには 100 行あり、データベースには 80 行しかありません。何が原因ですか?スクリプトからは考えていません。そうしないと、ブラウザに完全な結果が表示されません。

  • PDO (以下のコードを参照) を試しましたが、接続は成功しましたが、データベースにデータが入力されていません。構文エラーはありますか?

データベースにすべての行が欠落していない限り、 mysql_connectmysqli_connectまたはPDOを使用してもかまいません。

使用する接続文字列と行が欠落している理由を教えてください。スクリプトはうまく機能しており、ブラウザーに返されたデータは、スクレイピングされた URL と同じです。

コード 1 : mysqli_connect メソッドの使用+ スクリプトの概要

 $connection = mysqli_connect("localhost", "username", "password", "test");


 if (!$connection) {
printf("Connect failed: %s\n", mysqli_connect_error());
 exit();
  }

printf("Host information: %s\n", mysqli_get_host_info($connection));



$OutPut_Excel = $_GET['xls'];// 0 web page or 1 output excel
$show_Country = $_GET['show'];// 0 or 1
$urls = "
http://www.URL.com
";

 //output excel
if($OutPut_Excel == "1")
{
header("Content-Type: application/vnd.ms-execl;charset=iso-8859-1");
header("Content-Disposition: attachment; filename=data.xls");
header("Pragma: no-cache");
header("Expires: 0");
 }

set_time_limit(0);
ini_set('memory_limit', '-1');
ini_set('display_errors',true);


//output html
if($OutPut_Excel == "0")
{
?>
<html>
<head>
<title>scraper</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
 }

 if($OutPut_Excel == "0")
{
if($show_Country)
{
    $nameCountry = '<td>Country</td>';
}
 echo "<table border='1'><tr><td>Name</td><td>Surname</td>
<td>Email</td><td>Address</td><td>City</td><td>Profession</td>        <td>Phone</td>$Country</tr>";
 }
 else 
 {
echo "Name"."\t";
echo "Surname"."\t";
echo "Email"."\t";
echo "Address"."\t";
echo "City"."\t";
echo "Profession"."\t";
echo "Phone"."\t";
if($show_Country)
{
    echo "Country"."\t";
}

echo "\t\n";
    }

   include 'dom.php';
   $arrurl = explode(';',$urls);
   foreach ($arrurl as $u)
    {
$url = trim($u);
$html = file_get_html($url);
$string = $html->find("table[width=720]",0)->find("font[size=5]",0)->plaintext;
$arr = explode(' at ',$string);
$Satellite = trim($arr[0]);
$Degree = trim($arr[1]);


$k = 0;
foreach ($html->find("table[width=720]") as $d)
{
    $text = $d->outertext;
    $arr = explode('<td',$text);
    $out = "";
    foreach ($arr as $t)
    {
        $arr1 = explode('<font',$t);
        $count_font = count($arr1);
        $arr2 = explode('</font>',$t);
        $count_end_font = count($arr2);
        $need_count = $count_font-$count_end_font;
        $str = "";
        for($j=1;$j<=$need_count;$j++)
        {
            $str=$str."</font>";
        }
        if($out == "")
        {
            $out .= str_replace('</td>',$str,$t);
        }
        else 
        {
            $out .= "<td".str_replace('</td>',$str,$t);
        }
    }

ECT ... いくつかのコード

                            echo "$Name"."\t";
                            echo "$Surname"."\t";
                            echo "$Email"."\t";
                            echo "$Address"."\t";
                            echo "$City"."\t";
                            echo "$Profession"."\t";
                            echo "$Phone"."\t";
                            if($show_Country)
                            {
                                echo   "$Country"."\t";

    // SQL QUERY - WORKS FINE BUT SOME ROWS MISSING         
                        }
 $connection->query("INSERT INTO Directory (Name,Surname,Email,Address,City,Profession,Phone,Country) VALUES('$Name','$Surname','$Email','$Address','$City','$Profession','$Phone','$Country')");

コード 2 - PDO 接続 - 返されたデータがデータベースに入力されていません

$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');

try{

                         $stmt = $db->prepare('INSERT INTO Directory(Name,Surname,Email,Address,City,Profession,Phone,Country) VALUES($Name, $Surname, $Email, $Address, $City, $Profession, $Phone, $Country)');
                         $stmt->execute(array('Name' => $Name, 'Surname' => $Surname, 'Email' => $Email, 'Address' => $Address, 'City' => $City, 'Profession' => $Profession, 'Phone' => $Phone, 'Country' => $Country));
                         $affected_rows = $stmt->rowCount();

                         } catch(PDOException $ex) {
                         echo "An Error occured!"; 
                         some_logging_function($ex->getMessage());
                         }
4

2 に答える 2

0

試す

$stmt = $db->prepare('INSERT INTO Directory(Name,Surname,Email,Address,City,Profession,Phone,Country) VALUES($Name, $Surname, $Email, $Address, $City, $Profession, $Phone, $Country)');
                         $stmt->execute(array('Name' => $Name, 'Surname' => $Surname, 'Email' => $Email, 'Address' => $Address, 'City' => $City, 'Profession' => $Profession, 'Phone' => $Phone, 'Country' => $Country));

$stmt = $db->prepare('INSERT INTO Directory(Name,Surname,Email,Address,City,Profession,Phone,Country) VALUES(:Name, :Surname, :Email, :Address, :City, :Profession, :Phone, :Country)');
                         $stmt->execute(array(':Name' => $Name, ':Surname' => $Surname, ':Email' => $Email, ':Address' => $Address, ':City' => $City, ':Profession' => $Profession, ':Phone' => $Phone, ':Country' => $Country));

http://www.php.net/manual/fr/pdo.prepare.php

于 2013-01-29T15:54:36.743 に答える
0

mysql_query() 関数呼び出しで、データベース接続 $conn を提供する必要があります。

次のようになります。

$sql = mysql_query("your sql query", $conn);

すべての行が挿入されているわけではありません。挿入中のエラーである必要があります。MySQL エラーが出力されるように、次のようにコードを変更してみてください。

$connection->query("your sql query") or die(mysqli_error($connection));

ほとんどの場合、挿入する値のどこかにアポストロフィ ( ' ) が含まれています。それらを削除またはエスケープする必要がある場合があります。

于 2013-01-29T15:19:46.903 に答える