jaw
jaw

Reputation: 153

PDO query errors

If I change the first line of my PDO code below to "SELECT count(*) from writing AS posts" I get errors that the writing.ID and writing.approved columns do not exist (which they do). And if I get rid of the AS posts, then I just get Call to undefined function execute()

Can anyone see what I am doing wrong?

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

Upvotes: 1

Views: 26

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562578

Two issues going on...

If you define table aliases, you must reference columns using the alias (or unqualified).

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

Regarding the undefined function:

$stmt-->execute();

What you did was $stmt-- > execute() which compares if $stmt is greater than the result of the non-object-oriented function execute().

You must use only one dash with the object-oriented method invocation syntax:

$stmt->execute();

Upvotes: 1

Related Questions