I have the following sql statement, which works perfect and since I’m transferring everything to PDO, I’ve having the worst time trying to convert this complicated statement. Here it is working below:
$sql_res=mysql_query("SELECT a.first_name, a.last_name, a.uid, a.hometown,
IF(b.friend_two IS NULL, 0, 1) AS isFriend FROM users a LEFT JOIN friends
b ON a.uid = b.friend_two AND b.friend_one = $uid WHERE
CONCAT(a.first_name, ' ', a.last_name) LIKE '%$q%' AND a.uid <> $uid
ORDER BY isFriend DESC");
I’ve been trying for hours to convert and this is all I could get from another S.O example (it’s not even close to working)
Any help is wonderfully appreciate. Oh and also $q is a POST variable from the user so PDO is a must in my case.
UPDATED Code (2nd time):
$sql = "select a.first_name, a.last_name, a.uid, a.hometown,
IF(b.friend_two IS NULL, 0, 1) AS isFriend FROM users a LEFT JOIN
friends b ON a.uid = b.friend_two AND b.friend_one = :friend_one
where ( first_name like concat('%', :fname, '%') or last_name like
concat('%', :lname, '%') ) and a.uid <> :uid ORDER BY isFriend DESC";
$stmt= $db->prepare($sql); $stmt->bindValue(':fname', "%$q%", PDO::PARAM_STR);
$stmt->bindValue(':lname', "%$q%", PDO::PARAM_STR);
$stmt->execute(array(':friend_one' => $uid,':uid' => $uid));
This works without any error but fails to produce the same results as the original sql statement.
removed the extra
fromand put()around your name filter, asA OR B AND C=A OR (B AND C), and i gues you wanted(A OR) B AND C