我正在使用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”附近使用正确的语法
我感到困惑,我不知道这个查询有什么问题,以及如何解决它。
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
之后
在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;";
更改
"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解析为字符串或变量的一部分。
我没有测试过,但是timeStamp听起来像是mysql的保留字,
也许你可以试试这个领域的正确引用,或者最佳实践改变领域的名称。