I have the below PHP script which generates a MySQL query and runs it, however currently the query is returning an empty result.
$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);
Is for example giving the output JSON array:
{
"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
}
However if I run the generated query (replacing the ? with the right values) against the database manually I am getting the expected result (10 results returned)
Update
Even changing the SQL generation to hardcoded values instead of ? I still get no result.
i.e
{
"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
}