2017-07-05 122 views
0

我使用PHP錯誤和PDOSQLSTATE [42000]:語法錯誤或訪問衝突:1064您有

這裏是我的代碼:

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服務器版本的權利 語法使用近「ORDER BY TimeStamp DESC」在列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

+0

噢謝謝你:) – arteem

+0

歡迎:) – Exprator

+0

我是新的堆棧溢出我尋找像或某事,以表明它幫助我,但我無法找到如何接受答案? – arteem

0

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;"; 
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的保留字,

也許你可以試試這個領域的一個正確的報價,或最好的初步實踐變化該字段的名稱。

相關問題