1

请在下面找到一个脚本,该脚本会抓取应在我的数据库 ( SQL|PhpMyAdmin ) 中返回数据的 HTML 表。我使用(XAMPP)在浏览器中运行脚本

刮板工作正常,它可以在浏览器中显示数据或下载为 excel 文件。我使用 [ $conn = mysql_connect ]向 SQL 添加了一个连接字符串,但是,我读到此方法已被弃用,最好使用 [ mysqli_connect ] 或PDO

问题 :

  • 如果我使用 mysqli_connect,我不会遇到连接到我的数据库的问题,但返回的数据缺少一些行。例如,该表有 100 行,而我只会在数据库中找到 80 行。可能是什么原因造成的?我不从脚本中思考,否则我不会在浏览器中看到完整的结果

  • 我尝试了 PDO(见下面的代码),连接成功,但是我的数据库中没有输入任何数据。有没有语法错误

我不介意使用mysql_connectmysqli_connectPDO,只要我的数据库中的所有行都没有丢失任何内容。

请告知要使用什么连接字符串以及为什么缺少行。该脚本运行良好,浏览器中返回的数据与抓取的 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

在您的 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 回答
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 回答