我有下面的PHP腳本生成一個MySQL查詢並運行它,但是目前查詢返回一個空結果。PDO返回空結果
$sth = $dbh->prepare("SELECT TeamID FROM UserTeam WHERE UserID=? AND Deleted IS NULL");
$sth->execute(array($_POST['userID']));
$teams = $sth->fetchAll();
$sql = "SELECT * FROM Event WHERE UserID=? AND Deleted IS NULL";
if (count($teams) > 0) {
$sql .= " OR TeamID IN (";
foreach ($teams as $team){
$sql .= $team['TeamID'] . ",";
}
$sql = substr($sql, 0, -1);
$sql .= ")";
}
$sql .= " ORDER BY Created DESC LIMIT ?, 10 ";
$sth = $dbh->prepare($sql);
$sth->execute(array($_POST['userID'], $_POST['start']));
$events = $sth->fetchAll();
$response["success"] = 1;
$response["result"] = $events;
$response["sql"] = $sql;
$response["post"] = $_POST;
echo json_encode($response);
是例如給輸出JSON陣列:
{
"post" : {
"start" : "0",
"userID" : "33"
},
"result" : [
],
"sql" : "SELECT * FROM Event WHERE UserID=? AND Deleted IS NULL OR TeamID IN (12,13,17) ORDER BY Created DESC LIMIT ?, 10 ",
"success" : 1
}
但是,如果我運行生成的查詢(?用正確的值替換)對數據庫手動我得到預期的結果( 10個返回結果)
更新
即使在更改SQL代硬編碼值 代替 ?我仍然沒有結果。
即
{
"post" : {
"start" : "0",
"userID" : "33"
},
"result" : [
],
"sql" : "SELECT * FROM Event WHERE UserID=33 AND Deleted IS NULL OR TeamID IN (12,13,17) ORDER BY Created DESC LIMIT 0, 10 ",
"success" : 1
}
試圖通過一個變量,而不是'$ _ POST [「用戶ID」]',你也可以通過你的連接'$ dbh->的setAttribute(PDO :: ATTR_ERRMODE,PDO後添加此啓用錯誤方式: :ERRMODE_EXCEPTION);' –
直接在數據庫中運行查詢以查看是否出現錯誤。 – TurtleTread
已經說過它對數據庫正確工作 –