我有这个问题:
SELECT users.username, users.profile_img, m.address, m.id
FROM friend f
INNER JOIN users ON f.user_acc = users.username
INNER JOIN maps m ON users.username = m.username
WHERE (f.user_req = 'john' AND f.status = 1)
UNION DISTINCT
SELECT users.username, users.profile_img, m.address, m.id
FROM friend f
INNER JOIN users ON f.user_req = users.username
INNER JOIN maps m ON users.username = m.username
WHERE (f.user_acc = 'john' AND f.status = 1)
ORDER BY id DESC
当我从phpmyadmin手动运行时,此查询有效。 但是,当我从PHP代码运行它不再有效。 我用这种方式改造它:
$stmt = $db->prepare("SELECT `users.username`, `users.profile_img`, `m.address`, `m.id`
FROM `friend` `f`
INNER JOIN `users` ON `f.user_acc` = `users.username`
INNER JOIN `maps` `m` ON `users.username` = `m.username`
WHERE (`f.user_req` = '".$username."' AND `f.status` = '1')
UNION DISTINCT
SELECT `users.username`, `users.profile_img`, `m.address`, `m.id`
FROM `friend` `f`
INNER JOIN `users` ON `f.user_req` = `users.username`
INNER JOIN `maps` `m` ON `users.username` = `m.username`
WHERE (`f.user_acc` = '".$username."' AND `f.status` = '1')
ORDER BY `id` DESC");
$ username替换'john'。 错误在哪里?我认为报价是正确的.. 谢谢!