PDO查询错误

时间:2014-08-20 19:47:43

标签: pdo

如果我将下面的PDO代码的第一行更改为"从编写AS帖子"中选择SELECT count(*)我得到的错误是writing.ID和writing.approved列不存在(他们这样做)。如果我删除了AS帖子,那么我只是调用未定义的函数execute()

谁能看到我做错了什么?

$sql ="SELECT count(*) from writing
LEFT JOIN stories on writing.SID = stories.SID
LEFT JOIN wp_users ON writing.ID = wp_users.ID
WHERE (wp_users.ID != $user_ID) AND (writing.approved = 'Y') ";

$stmt = $dbh->prepare($sql);
$stmt->bindParam(':wp_users.ID', $user_ID, PDO::PARAM_INT); 
$stmt->bindParam(':stories.ID', $ID, PDO::PARAM_INT); 
$stmt->bindParam(':writing.ID', $ID, PDO::PARAM_INT); 
$stmt->bindParam(':writing.WID', $WID, PDO::PARAM_INT); 
$stmt->bindParam(':approved', $j = Y, PDO::PARAM_STR); 
$stmt->bindParam(':position', $position, PDO::PARAM_INT); 
$stmt-->execute();
$therow = $stmt-->fetchAll(PDO::FETCH_ASSOC);

1 个答案:

答案 0 :(得分:1)

两个问题正在进行......

如果定义表别名,则必须使用别名(或不合格)引用列。

SELECT table.column FROM table /* RIGHT */

SELECT a.column FROM table AS a /* RIGHT */

SELECT column FROM table /* RIGHT */

SELECT column FROM table AS a /* RIGHT */

SELECT table.column FROM table AS a /* WRONG */

SELECT a.column FROM table /* WRONG */

关于未定义的函数:

$stmt-->execute();

你所做的是$stmt-- > execute(),它比较$stmt是否大于非面向对象函数execute()的结果。

您必须只使用一个带有面向对象方法调用语法的短划线:

$stmt->execute();