2015-11-09 88 views
0

夥計們我試圖在這裏實際創建一個像Facebook的飼料這樣的飼料,但我得到這個錯誤,我不知道我哪裏會出錯。 這裏是代碼:如何糾正「SQLSTATE [42000]:語法錯誤或訪問衝突:1064」錯誤

$sql3="select u.update_id, u.update_body,u.account_name,u.os_id,u.author,u.time,u.title," 
      . "c.comment_body, c.os_id,c.author,c.time" 
      . "from updates as u, comment_update as c " 
      . "where c.os_id=u.update_id and u.account_name = ':session' and u.type in ('a','c') and u.account_name=':friend' and u.type =('a'|'c') order by u.time asc,c.time desc"; 
    $stmth=$conn->prepare($sql3); 
    $stmth->execute(array(":session"=>$_SESSION['uname'],":friend"=>$friend)); 
    $status_reply= $stmth->fetchAll(PDO::FETCH_ASSOC); 

這裏是錯誤代碼: -

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as u, comment_update as c where c.os_id=u.update_id and u.account_name = ':sessi' at line 1' in /opt/lampp/htdocs/project-chg/status&comments.php:28 Stack trace: #0 /opt/lampp/htdocs/project-chg/status&comments.php(28): PDOStatement->execute(Array) #1 /opt/lampp/htdocs/project-chg/example1.php(30): include('/opt/lampp/htdo...') #2 {main} thrown in /opt/lampp/htdocs/project-chg/status&comments.php on line 28

任何幫助,將不勝感激。

+0

您需要在您的查詢 「從」 前添加一個空格。 – Thevenin

+0

@Thevenin感謝兄弟,解決了錯誤,但現在又出現了一個新的錯誤'致命錯誤:帶有'SQLSTATE [21000]:基數違規:1241操作數應該包含1列在/ opt'的未捕獲異常'PDOException' /lampp/htdocs/project-chg/status&comments.php:28' – shan2batman

回答

0

您的代碼:

$sql3="select u.update_id, u.update_body,u.account_name,u.os_id,u.author,u.time,u.title," 
      . "c.comment_body, c.os_id,c.author,c.time" 
      . "from updates as u, comment_update as c " 
      . "where c.os_id=u.update_id and u.account_name = ':session' and u.type in ('a','c') and u.account_name=':friend' and u.type =('a'|'c') order by u.time asc,c.time desc"; 
  1. 添加從

  2. 更改之前的空間('a'|'c')('a','c')

  3. 考慮使用逗號語法JOIN語法instad

  4. 也許你需要刪除'周圍:friend:session

  5. WHERE clause conditions是我u.account_name = :session and u.account_name=:friend同一列很奇怪??? +一倍u.type條件

結果:

$sql3="select u.update_id, u.update_body,u.account_name,u.os_id,u.author,u.time,u.title," 
      . "c.comment_body, c.os_id,c.author,c.time" 
      . " from updates as u JOIN comment_update as c ON c.os_id=u.update_id " 
      . "where u.account_name = :session and u.type in ('a','c') and u.account_name=:friend and u.type =('a','c') order by u.time asc,c.time desc"; 
+0

我仍然得到相同的錯誤兄弟! – shan2batman

+0

@ shan2batman準備http://sqlfiddle.com – lad2025

+0

這裏是小提琴「http://sqlfiddle.com/#!2/75908」 – shan2batman

相關問題