0

This is mysql code im trying to run:

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;

Im getting this error :

/* 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 */

My aim is to get id, courseid, title, info, lasteditedby from CourseInformation table, and then Id, forename and surname from user table. Where the userid is the same as lasteditedby.

I really can't see what sql syntax is wrong as i've used

:courseid

in other pdo sql querys that ive run


for reference, this is my php code with that sql in

        $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 />';
        }

Could anyone please point me in the direction as to what is wrong? Thanks for reading

4

1 回答 1

0

您还没有创建语句句柄,而是创建了一个字符串。

教程建议这样做:

$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会出现错误,这表明您可能需要更好的示例来工作,因为这似乎是同时应用了一些相互矛盾的技术。

于 2013-03-05T20:49:56.540 回答