![](/img/trans.png)
[英]Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
[英]SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error
我正在使用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”附近使用
我很困惑,我不知道此查询有什么问题以及如何解决。
在limit
子句之前使用orderby
子句。
$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;";
更改
"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解析为字符串还是变量的一部分。
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
后设置您的限额
我尚未测试,但timeStamp听起来像mysql的保留字,
也许您可以尝试使用此字段的正确引号,或者最好更改该字段的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.