这个查询应该工作吗?如果没有,我怎么能在 PHP 中使用 PDO 将一个查询放入另一个查询?
这是我的查询:
$userid = array($users->school_name_from_id($_SESSION['user_id']), $_SESSION['user_id'], $_SESSION['user_id']);
$statementString =
'SELECT * FROM schoolinfo
WHERE schoolinfo.school = ? AND
SELECT count(*) FROM friendstable
WHERE friendstable.user_ID = ?
AND frienstable.friend_ID = schoolinfo.id <= 0
AND
SELECT count(*) FROM friendstable
WHERE friendstable.user_ID = schoolinfo.id
AND friendstable.friend_ID = ? <= 0
ORDER BY RAND()
LIMIT 6';
$stmt = $database->_link->prepare($statementString);
$stmt->execute($userid);
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($array);
这是我在运行时得到的完整错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT count(*) FROM friendstable WHERE friendstable.user_ID = '1' AND frienstab' at line 1' in C:\xampp\htdocs\thinkaztech\PHP\getSocialInformation.php:20 Stack trace: #0 C:\xampp\htdocs\thinkaztech\PHP\getSocialInformation.php(20): PDOStatement->execute(Array) #1 {main} thrown in C:\xampp\htdocs\thinkaztech\PHP\getSocialInformation.php on line 20