带有消息SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064

时间:2014-01-22 23:18:49

标签: php mysql pdo

这是我的疑问:

 public function getAllServices($start, $limit)
{
    $services = array();

    $q = $this->init()->prepare('SELECT id, service_title, time_add FROM services ORDER BY id DESC LIMIT :start, :limit');
    $q->execute(array(":start" => $start, ":limit" => $limit));

    while ($values = $q->fetchAll(PDO::FETCH_ASSOC))
        $services[] = $values;

    return $services;
}

错误讯息:

  

致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在“0”,“10”附近使用正确的语法

3 个答案:

答案 0 :(得分:0)

根据语法错误消息,我会说它引用传递给LIMIT的整数,这是不允许的。

答案 1 :(得分:0)

只是为此添加一些其他信息 - 我遇到了类似的问题,并且已经在Google上搜索了一段时间。我发现PDO参数化存在重复参数的问题:

$q = $this->init()->prepare("
    (SELECT * FROM my_table WHERE date > :date LIMIT :limit)
    UNION
    (SELECT * FROM their_table WHERE date > :date LIMIT :limit)
");
$q->bindValue(':date', $somedate, PDO::PARAM_STR);
$q->bindValue(':limit', $limit, PDO::PARAM_INT);
$q->execute();

此代码生成的查询不会转义第一个:limit ,但会错误地转义第二个:limit ,例如:

(SELECT * FROM my_table WHERE date > '2014-04-14' LIMIT 20)
UNION
(SELECT * FROM their_table WHERE date > '2014-04-14' LIMIT '20')

你在很多这些类似的问题中提到 LIMIT 的原因是为字段比较提供一个转义整数不会破坏MySQL中的任何内容,但做同样的事情是为了LIMIT确实。

因此,虽然这并没有具体回答OP的问题,但我想像我这样的很多人最终都会遇到同样的问题。

我目前没有这方面的整洁解决方案,因此我采用了独特的参数,例如:limit1 :limit2 。也许有人可以建议更好的解决方案?

答案 2 :(得分:0)

OP解决方案。

解决了这个问题:

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Private Sub DownloadPic()

    Dim url As String
    Dim fileLocation As String

    url = "https://maps.googleapis.com/maps/api/staticmap?size=600x600&path="
    url = url & "enc%3Amym~h?g~uz??e?a?q?a?k?c?u?e?s?i?}?m?{?c?s?a?i?a?i?a?i?a?i?a?g?a?o?a?g?c?q?a?i?g?c?a?i?m?w?a?q?q?s?c?i?s?u?e?q?m?y?e?q?o?}?q?w?a?g?o?w?c?g?e?q?a?g?k?o?o?y?m?w?q?w?a?g?q?u?a?g?i?y?m?s?a?g?k?y?a?g?i?w?a?g?e?{??q?c?y?g?w?a?e?e?u?g?g?m?}?a?g?m?{?o?y?k?a?i?a?i?c?o?s?u?q?i?y?o?}?c?y?c?k?a?o?k?{?c?o?o?w?a?g?m?w?m?y?a?g?m?g?i?y?q?{?c?o?e?u?o?m?a?g?e?c?d?u?f?c?z??b?b??b?f`?a?f??h?`?z??h`?d?|??z?c?f`?i?j??d`??d??z??p?c?d`?i?d??h`?c?h??d`?c?z?a?b??d`?c?~?i?v?k?h?c?b`?k?j?e?h??~?n?b`?c?d??d`?i?b`?u?f?u?`?o?e?u`?q?w`?q?w`?c?e?q?g`?o?y?k?k?}?y?w?g`?s?}?g?i?}?i`?e?e?u?g`?a?e?s?m`?i?c`?c?o?e?{?a?m??k?e?c`?e?a`?a?g?u?o`?a?g?a?e?m?i`?i?{???q?o`?k?w?e?m?m?m`?m?i`?u?m`?c?e?e?m?s?u`??q?i?y?y?k`?a?e?u?q`?q?w`?a?i?q?w`?m?w`?u?u`?s?s`?u?s`?c?g?s?q`?s?q`?e?k?m?m`?q?u`?c?e?o?c`?g?}?b?ca?c?{?m?c?o?n?w?|?c?b?u?r?y?d`?q?v`?s?|`?u?r`?g?n?k?v?g?l?i?n?u?m?a`?u?e?a?g`?y?e?c?e`?y?e?c?g`?u"
    url = url & "?i?e?c`?{?e?c?a`?y?g?e?a`?e`?{?}?{?h?a`?v?}?~?a?d?m?f`?g?n`?i?~`?e?z`?o?t`?c?f?m?|`?a"
    url = url & "?f?i?|`?k?v`?g?|`??v?a?~?c?z?`?ba??|?c?d?}?r?}?f`?q?r?e?l?m?l?a`?z?i?d?}?n?y?h?g?j?e?l?e?v?g?x`??|`?d?|`?`?l`?e?v`?d?z`?`?n?j?x`??f?h?z`??f?j?|`?b?t?f?v`?h?v`??f?p?|`?d?p?h?t`??v`?g?|`?c?p??~`?b?l?f??n??`?d?n?x?h?n?p?d`?r?l`?j?v?r?l`?t?p`?h?r?b?d?f?r?f?x?b?l?`?l?b?l?f?d`?`?f?b?h`?`?z??h?c?l`??f?c?l`?j?p`?n?r`?b?f?t?r`?d?n?f?v`?m?z?{?~?s?|?a`?|?a`?v?c?`?k?j?m?n?a`?z?c?b?a`?x?c`?|?c?b?a`?|?q?n?a`?z?e?b?y?x?{?~?`?d`?`?z?}?f`?i?d`??f??l`??r`?v?l`?t?p`?`?f?x?n`?t?~?x?d`?v?b`?j?t?`?b?j?p?h?j?b?b?z??v??h?j`??p`??f?j?z?|?b?v?a?h?b?v?r?z?m??w?b?c?~?{?f?e?b?}?``?q?h?a?d?w?v?d?"
    fileLocation = "C:\Users\User\Desktop\TestFolder\Test.png"

    If URLDownloadToFile(0, url, fileLocation, 0, 0) = 0 Then
        Debug.Print "downloaded"
        ThisWorkbook.Worksheets("Sheet1").Range("A1").Parent.Pictures.Insert fileLocation
        Kill fileLocation
    Else
        Debug.Print "failed"
    End If

End Sub