I'm having issues implementing an SQL query using PDO.
$friend_emails = $pdo->prepare("SELECT DISTINCT User2
FROM members
WHERE User1 = '$user'
UNION
SELECT DISTINCT User1
FROM members
WHERE User2 = '$user'");
$friend_emails->execute();
for($i=0; $row = $friend_emails->fetch(); $i++) {
echo "foo";
}
"foo" doesn't show at all. I var_dumped $row and $friend_emails->fetch() both of which are a
boolean false
so I'm not exactly sure why that is, I thought it would return an array of the data.
Any help is greatly appreciated!
execute()
call check$friend_emails->errorInfo()
for any error messages. Besides, if you use aprepare()
statement, you should not insert the dynamic values there, but in theexecute()
later on. – Sirko May 14 '13 at 9:37prepare
when you are using String Interpolation and not reallyprepare
ing your statement? – Hanky 웃 Panky May 14 '13 at 9:38errorInfo()
returns an array with information about what went wrong. You could usevar_dump()
to have a look at it outputs in debugging. In a productive system you should have a more sophisticated error handling. – Sirko May 14 '13 at 9:43