【问题标题】:how to rectify "SQLSTATE[42000]: Syntax error or access violation: 1064 " error如何纠正“SQLSTATE [42000]:语法错误或访问冲突:1064”错误
【发布时间】:2015-11-09 14:03:28
【问题描述】:

伙计们,我正在尝试在这里创建一个类似于 facebook 的提要的提要,但我收到了这个错误,我无法弄清楚我哪里出错了。 这是代码:

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

这是错误代码:-

致命错误:未捕获异常 'PDOException' 并带有消息 'SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在 /opt/lampp 中的 'as u, comment_update as c where c.os_id=u.update_id and u.account_name = ':sessi' at line 1' 附近使用/htdocs/project-chg/status&cmets.php:28 堆栈跟踪:#0 /opt/lampp/htdocs/project-chg/status&cmets.php(28): PDOStatement->execute(Array) #1 /opt/lampp/htdocs /project-chg/example1.php(30): include('/opt/lampp/htdo...') #2 {main} 在第 28 行的 /opt/lampp/htdocs/project-chg/status&cmets.php 中抛出

任何帮助将不胜感激。

【问题讨论】:

  • 您需要在查询中的“from”之前添加一个空格。
  • @Thevenin 感谢兄弟解决了错误,但现在出现了一个新错误,如下所示Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)' in /opt/lampp/htdocs/project-chg/status&comments.php:28

标签: php mysql pdo


【解决方案1】:

您的代码:

$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. 在from前加空格

  2. ('a'|'c')更改为('a','c')

  3. 考虑使用JOIN 语法代替逗号语法

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

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2014-01-14
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多