PHP:PDO连接SQL和缺少的返回数据

PHP:PDO连接SQL和缺少的返回数据,php,sql,xaml,phpmyadmin,Php,Sql,Xaml,Phpmyadmin,请在下面找到一个脚本,该脚本将删除一个HTML表,该表应返回数据库中的数据(SQL | PhpMyAdmin)。我使用(XAMPP)在浏览器中运行脚本 刮板工作正常,它可以在浏览器中显示数据或作为excel文件下载。我使用[$conn=mysql\u connect]向SQL添加了一个连接字符串,但是,我读到这种方法已被弃用,最好使用[mysqli\u connect]或PDO 问题: 如果我使用mysqli_connect,我在连接数据库时不会遇到问题,但是返回的数据缺少一些行。例如,该表有

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

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

问题:

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

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

我不介意使用mysql\u-connectmysqli\u-connectPDO,只要数据库中的所有行都没有遗漏

请告知要使用的连接字符串以及缺少行的原因。该脚本运行良好,浏览器中返回的数据与刮取的URL相同

代码1:使用mysqli\u连接方法+脚本概述

 $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);
        }
    }
代码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());
                         }
在mysql_query()函数调用中,需要提供数据库连接$conn

应该是这样的:

$sql = mysql_query("your sql query", $conn);
如果没有插入所有行,则插入过程中一定出错。尝试这样修改代码,以便打印MySQL错误:

$connection->query("your sql query") or die(mysqli_error($connection));
很可能是插入的某个值中的某个撇号(')。您可能需要删除或退出它们。

试试看

$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));


阅读文档,您的示例仍然使用mysql函数,而不是代码1标题中所说的mysqli函数。是的,很抱歉,我复制了错误的脚本。更正。正如我发布的那样,mysqli_connect工作正常,但返回的数据在我的数据库中丢失。我想知道为什么?这是配置问题吗?如果它来自脚本,我将在浏览器中获取所有行。。我不是专家,这就是我在这里发帖的原因。谢谢你的关注,非常感谢乔纳森。PDO连接正在工作。但是,我的数据库中有82行(共100行)。。为什么您认为缺少行(我尝试了3种方法)。是来自脚本还是数据库设置?不知道,是否有
唯一的
字段?
$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));