SQLSTATE [42000]:语法错误或访问冲突:1064您有错误

问题描述 投票:0回答:4

我正在使用php和pdo

这是我的代码:

try {
    $sql="LOCK TABLE appleid WRITE, appleid AS appleid1 READ;";
    $stmt = $GLOBALS['$connection']->prepare($sql);
    $stmt->execute();
    $sql="SELECT MAX(num) FROM appleid;";//to know how many rows it has
    $stmt = $GLOBALS['$connection']->prepare($sql);
    $stmt->execute();
    $result=$stmt->fetch();
    $table_top=$result[0];

    if (empty($head)) $head = 1;
    $check=$table_top - $head;
    $check++;//number of available rows that are ready to use

    if($check>=$this->apple_id_num)
    {
        $sql = "SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM 
        appleid AS appleid1
        WHERE num>=$head LIMIT $this->apple_id_num ORDER BY `TimeStamp` 
        DESC;";
        $stmt = $GLOBALS['$connection']->prepare($sql);
        $stmt->execute();
        $this->pre_head=$head;      
        $head=1+$head+$this->apple_id_num;
        $sql="UNLOCK TABLES;";
        $this->num_rows = $stmt->rowCount();
        echo $stmt->rowCount();
    }
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

我收到此错误:

SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM appleid AS appleid1 WHERE num> = 1 LIMIT 1 ORDER BY TimeStamp DESC; SQLSTATE [42000]:语法错误

或访问冲突:1064您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第2行的“ORDER BY TimeStamp DESC”附近使用正确的语法

我感到困惑,我不知道这个查询有什么问题,以及如何解决它。

php pdo locking max
4个回答
0
投票
SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM 
        appleid AS appleid1
        WHERE num>=$head ORDER BY `TimeStamp` 
        DESC LIMIT $this->apple_id_num ; 

把你的限制放在orderby之后


0
投票

orderby子句之前使用limit子句。

$sql = "SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM 
appleid AS appleid1
WHERE num>=$head ORDER BY `TimeStamp` DESC LIMIT $this->apple_id_num;";

0
投票

更改

"SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM 
    appleid AS appleid1
    WHERE num>=$head LIMIT $this->apple_id_num ORDER BY `TimeStamp` 
    DESC;";

"SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM 
    appleid AS appleid1
    WHERE num>=$head LIMIT {$this->apple_id_num} ORDER BY `TimeStamp` 
    DESC;";

因为双引号不知道是否将 - > apple_id_num解析为字符串或变量的一部分。


0
投票

我没有测试过,但是timeStamp听起来像是mysql的保留字,

也许你可以试试这个领域的正确引用,或者最佳实践改变领域的名称。

© www.soinside.com 2019 - 2024. All rights reserved.