繁体   English   中英

MySQL查询无法执行

[英]MySQL query fails to execute

我正在尝试向服务器查询大量数据,这是我的代码:

$queryString = "";

$connect = mysqli_connect("localhost", "username", "password", "database");

$loopLength = 20;
$currentGroup = 1;
$currentLoopAmount = 0;
$queryAmount = 5;

for($v = 0; $v < ceil($loopLength / $queryAmount); $v++){
    //echo "Looping Main: " . $v . "<br>";
    //echo $loopLength - (($currentGroup - 1) * 10) . "<br>";
    if($loopLength - (($currentGroup - 1) * $queryAmount) >= $queryAmount){
        $currentLoopAmount = $queryAmount;
    }
    else{
        $currentLoopAmount = $loopLength - (($currentGroup - 1) * $queryAmount);
    }

    //echo $currentLoopAmount;

    $queryString = "";

    for($q = (($currentGroup - 1) * $queryAmount); $q < $currentLoopAmount + (($currentGroup - 1) * $queryAmount); $q++){
        //echo "&nbsp;&nbsp;Looping Sub: " . $q . "<br>";
        $tempVariable = grabPageData($URLs[$q], $q);

        $queryString .= $tempVariable;
        if($q < $loopLength-1){
            $queryString .= ",";
        }
        else{
            $queryString .= ";";
        }
    }

    echo $queryString;

    $query = "INSERT INTO PublicNoticesTable (url, county, paperco, date, notice, id) VALUES " . $queryString;
    $result = mysqli_query($connect, $query);

    if($result){
        echo "Success";
    }
    else{
        echo "Failed : " . mysqli_error($connect) . "<br>";
    }

    $currentGroup += 1;
}

$loopLength变量是动态的,可以是数千个,也可能是数十万个。 我设计此函数是为了将大量数据分成一批较小的查询,因为我无法一次通过GoDaddy将数据上传到共享托管服务上。 $queryAmount变量表示较小查询的大小。

这是一个插入表中的值集之一的示例:这是从公共通知中获取的数据,我的代码是在grabPageData()函数中检索到的。

('http://www.publicnoticeads.com/az/search/view.asp?T=PN&id=37/7292017_24266919.htm','Pima','Green Valley News and Sun','2017/07/30',' ___________________________ARIZONA SUPERIOR COURT, PIMA COUNTYIn the Matter of the Estate of:JOSEPH T, DILLARD, SR.,Deceased.DOB: 10/09/1931No. PB20170865NOTICE TO CREDITORS(FOR PUBLICATION)NOTICE IS HEREBY GIVEN that DANA ANN DILLARD CALL has been appointed Personal Representative of this Estate. All persons having claims against the Estate are required to present their claimswithin four months after the date of the firat publication of this notice or the claims will be forever barred. Claims must be presented by delivering or mailing a written statement of the claim to the Personal Representative at the Law Offices of Michael W. Murray, 257 North Stone Avenue, Tucson, Arizona 85701.DATED this 17th day of July, 2017./S/ Micahel W. MurrayAttorney for the Personal RepresentativePub: Green Valley News & SunDate: July 23, 30, August 6, 2017 Public Notice ID: 24266919',' 24266919'),

为了获得这些数据,我通过一个对页面进行爬网并抓取的函数来运行它。 然后,我通过此功能将网页html代码放入:

function cleanData($data){
    $data = strip_tags($data);
    //$data = preg_replace("/[^a-zA-Z0-9]/", "", $data);
    //$data = mysql_real_escape_string($data);
    return $data;
}

如上所示,这给了我没有标签的内容。 这是问题所在。

该函数执行,一切似乎都花哨。 然后输出函数(取决于$queryAmount变量,出于问题$queryAmount ,我不会将其保留在10以上),就像您在函数中看到的那样...

Failed : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

奇怪的是,当我有大量数据时,比如说$loopLength变量像是116。结果将输出,“失败:(错误)失败:(错误)失败...(错误)成功。因此仅实际查询的最后一组数据??不确定。

我不确定如何解决此问题,我想重新审视。 有人可以帮我吗。 我已经在这个问题上工作了几个小时,试图找到解决方案。

很抱歉让这个问题困扰您:(

编辑:

我从以前的代码更改为使用mysql准备的语句,而不是...见下文:

$grabDataResults = [
        "url" => "",
        "county" => "",
        "paperco" => "",
        "date" => "",
        "notice" => "",
        "id" => "",
    ];

$connect = mysqli_connect("localhost", "bwt_admin", "Thebeast1398", "NewCoDatabase");

if($stmt = mysqli_prepare($connect, "INSERT INTO PublicNoticesTable (url, county, paperco, date, notice, id) VALUES (?, ?, ?, ?, ?, ?)")){

mysqli_stmt_bind_param($stmt, 'ssssss', $grabDataResults["url"], $grabDataResults["county"], $grabDataResults["paperco"], $grabDataResults["date"], $grabDataResults["notice"], $grabDataResults["id"]);

$loopLength = 1;

for($v = 0; $v < $loopLength; $v++){
    $grabDataResults = grabPageData($URLs[$v], $v);
    mysqli_stmt_execute($stmt);
    printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
    printf("Error:\n", mysqli_stmt_error($stmt));
    echo "(" . $grabDataResults["url"] . "," . $grabDataResults["county"] . "," . $grabDataResults["paperco"] . "," . $grabDataResults["date"] . "," . $grabDataResults["notice"] . "," . $grabDataResults["id"] . ")";
}

mysqli_stmt_close($stmt);

mysqli_close($connect);
}

不幸的是,这是我从输出中得到的:

1 Row inserted. 0 Error: 

实际上没有错误输出,并且已插入行。 但是,当我导航到数据库时,请查看已存储的值。它们都是空的。 echo语句输出以下内容:

(http://www.publicnoticeads.com/az/search/view.asp?T=PN&id=31/7292017_24266963.htm,Yuma,Sun (Yuma), The,2017/07/30,, 24266963)

因此,我知道所有变量都包含$notice变量以外的内容,由于某些原因该变量被我的cleanData()函数破坏了。

我在查询中看到的主要错误是查询本身。 您正在使用带有分隔字段和值的INSERT INTO。 但是您忘了在价值上使用花招。

请记住,INSERT INTO的用法如下:

First option:
INSERT INTO table field1 = value1, field2 = value2;

Second option:
INSERT INTO table (field1, field2) VALUES (value1, value2);

另外,请记住对每个字段和值进行转义以避免更多错误:示例:

First option:
INSERT INTO `table` `field1` = 'value1', `field2` = 'value2';

Second option:
INSERT INTO `table` (`field1`, `field2`) VALUES ('value1', 'value2');

如果使用的是mysqli驱动程序,则为了提高安全性,可以使用准备好的语句来自动转义您的值。 在这种情况下,查询的语法如下:

First option:
INSERT INTO `table` `field1` = ?, `field2` = ?;

Second option:
INSERT INTO `table` (`field1`, `field2`) VALUES (?, ?);

另外,除了使用mysqli_query(),还需要使用mysqli_prepare(),mysqli_bind_param()和mysqli_execute()。 您可以在此处检查有关它们语法的更多数据: http : //php.net/manual/en/mysqli.prepare.php

至少,您可以使用mysqli_real_escape_string()函数使输入正确转义和检查。 您可以在以下位置查看此功能的文档: http : //php.net/manual/zh/mysqli.real-escape-string.php

您的代码是正确的。 只是您需要添加()arround querystring Akso您需要删除; 从查询字符串结尾。 所以删除其他条件

else{
    $queryString .= ";";
}

更改您的查询,如:

$query = "INSERT INTO PublicNoticesTable (url, county, paperco, date, notice, id) VALUES (" . $queryString . ")";

另外建议使用准备好的语句来防止sql注入

您需要在获取数据之后并执行之前绑定数据...

$loopLength = 1;

for($v = 0; $v < $loopLength; $v++){
    $grabDataResults = grabPageData($URLs[$v], $v);

    mysqli_stmt_bind_param($stmt, 'ssssss', $grabDataResults["url"],
           $grabDataResults["county"], $grabDataResults["paperco"], 
           $grabDataResults["date"], $grabDataResults["notice"], 
           $grabDataResults["id"]);

    mysqli_stmt_execute($stmt);
    printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
    printf("Error:\n", mysqli_stmt_error($stmt));
    echo "(" . $grabDataResults["url"] . "," . $grabDataResults["county"] . "," . $grabDataResults["paperco"] . "," . $grabDataResults["date"] . "," . $grabDataResults["notice"] . "," . $grabDataResults["id"] . ")";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM