I'm facing a rather odd issue. I use PHP 7.0 with a MySQL 5.5 DB on Azure. When testing queries on the database, I use Sequel Pro to directly perform queries and check the result. From PHP, I use the exact same query (but with MySQLi prepared statements), but I receive an error when executing this query from PHP.
In particular, I have issues with a conditional INSERT statement where I define a temporary table in the FROM clause. The query in question is:
INSERT INTO tryouts (t_id, user_id, tryout_count)
SELECT entry.t_id, entry.user_id, 0
FROM
(SELECT ? AS t_id, ? AS user_id) AS entry,
tournaments
WHERE
(tournaments.t_id = entry.t_id) AND (tournaments.tryout_limit IS NOT NULL);
Replacing the '?' with actual values in Sequel Pro executes the query as expected: a new row is inserted.
However, when executing this query from PHP using MySQLi, I get the following error message:
Unknown column 'entry.t_id' in 'field list'
This is the PHP code used, of which the error_log is triggered with the above message:
$query = "
INSERT INTO tryouts (t_id, user_id, tryout_count)
SELECT entry.t_id, entry.user_id, 0
FROM
(SELECT ? AS t_id, ? AS user_id) AS entry,
tournaments
WHERE
(tournaments.t_id = entry.t_id) AND (tournaments.tryout_limit IS NOT NULL);";
$statement = $dbLink->stmt_init();
if (!$statement->prepare($query) ||
!$statement->bind_param('ii', $tournamentID, $userID) ||
!$statement->execute())
{
error_log("EnterTournamentDetails: Something went wrong while letting the user enter the tournament: ".$dbLink->error);
return NULL;
}
Anyone that can shine a light on the difference, or what that could mean? I face this same issue on other, similar insertion queries, but this one is one of the more simple examples I could find.
Thanks in advance!