如何纠正“SQLSTATE [42000]:语法错误或访问冲突:1064”错误

时间:2015-11-09 14:03:28

标签: php mysql pdo

伙计们我试图在这里实际创建像facebook这样的Feed,但是我得到了这个错误,我无法弄清楚我哪里出错了。 这是代码:

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

这是错误代码: -

  

致命错误:带有消息'SQLSTATE [42000]的未捕获异常'PDOException':语法错误或访问冲突:1064 SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在'/ u,comment_update附近使用正确的语法,其中c.os_id = u.update_id和u.account_name =':sessi'在/ opt / lampp的第1行' /htdocs/project-chg/status&comments.php:28堆栈跟踪:#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 ...')#opt {lamp}抛出/ opt / lampp / htdocs / project-chg / status& ;评论.php在第28行

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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条件

  6. 结果:

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