mysql查询错误sql语法pdo

时间:2013-03-05 20:42:24

标签: mysql sql syntax pdo

这是我试图运行的mysql代码:

SELECT i.id, i.courseid, i.title, i.info, i.lasteditedby, u.id, u.forename, u.surname
FROM courseinformation as i JOIN users AS u ON (i.lasteditedby = u.id)
WHERE i.courseid = :courseid
ORDER BY i.id desc LIMIT 2;

我收到此错误:

/* SQL Error (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 ':courseid ORDER BY i.id desc LIMIT 2' at line 1 */

我的目标是从CourseInformation表中获取id,courseid,title,info,lasteditedby,然后从用户表中获取Id,forename和surname。其中userid与lasteditedby相同。

我真的看不出sql语法错误,因为我已经使用了

:courseid

在我运行的其他pdo sql查询中


供参考,这是我在

中使用sql的php代码
        $courseid = 'G11111';
        $sql = "SELECT i.id, i.courseid, i.title, i.info, i.lasteditedby, u.id, u.forename, u.surname FROM courseinformation as i JOIN users AS u ON (i.lasteditedby = u.id) WHERE i.courseid = :courseid ORDER BY i.id desc LIMIT 2";
        $sql->bindParam(":courseid", $courseid);
                    $sql->execute();

        foreach ($db->query($sql) as $row) {
            echo '<div class="announceTitle">';
            echo $row['title'].'<br />';
            echo $row['forename'].' '.$row['surname'].'<br />';
            echo '</div>
                <div class="announceText">';
            echo $row['info'];
            echo '</div>
                <br />
                <br />';
        }

有人可以指出我的方向是什么问题吗? 感谢您的阅读

1 个答案:

答案 0 :(得分:0)

您尚未创建语句句柄,您已创建了一个字符串。

tutorial建议:

$sth = $db->prepare("SELECT i.id, i.courseid, i.title, i.info, i.lasteditedby, u.id, u.forename, u.surname FROM courseinformation as i JOIN users AS u ON (i.lasteditedby = u.id) WHERE i.courseid = :courseid ORDER BY i.id desc LIMIT 2");
$sth->bindParam(":courseid", $courseid);

稍后您使用$db->query执行此字符串是错误产生的位置,并且表明您可能需要更好的示例,因为这似乎是同时应用的一些相互矛盾的技术。