简体   繁体   中英

how to rectify “SQLSTATE[42000]: Syntax error or access violation: 1064 ” error

Guys i'm trying to actually create a feed like facebook's feed here but i get this error that i cant figure out where im going wrong. here is the code:

$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);

here is the error code:-

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

any help would be appreciated.

Your code:

$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. Add space before from

  2. Change ('a'|'c') to ('a','c')

  3. Consider using JOIN syntax instad of comma syntax

  4. Probably you need to remove ' around :friend and :session

  5. Whole WHERE clause conditions are very odd for me u.account_name = :session and u.account_name=:friend the same column??? + doubled u.type condition

Result:

$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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM