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

时间:2017-07-05 10:55:52

标签: php pdo locking max

我使用的是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&gt; = 1 LIMIT 1 ORDER BY TimeStamp DESC;   SQLSTATE [42000]:语法错误

     

或访问冲突:1064您的SQL语法出错;校验   与您的MySQL服务器版本对应的手册   要在TimeStamp DESC&#39; ORDER BY附近使用的语法在第2行

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

4 个答案:

答案 0 :(得分: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;";

答案 1 :(得分: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;";

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

答案 2 :(得分: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

之后设置限额

答案 3 :(得分:0)

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

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