Apparently,
SELECT * FROM (SELECT ? )
...is not recognized as a valid MySQL syntax. A table name is missing.
EDIT, Concerning your comments:
First of all, please note that executing this statement in a console by substituting ?
with a constant does not emulate your situation, so I would consider the result invalid for comparison.
But then again, executing it without substituting ?
would, naturally, give an error.
That's because executing just the select is irrelevant to your situation. In your php code, it's not the execution that fails but rather the preparation. So the proper way to emulate this using a console, would be the PREPARE
statement.
So doing a
PREPARE myStmt
FROM 'SELECT * FROM (SELECT ? ) AS tmp WHERE NOT EXISTS (
SELECT Identifier FROM eeg WHERE Identifier = ?
) LIMIT 1'
would reproduce your issue more accurately.
Now, it seems that PREPARE
has a difficulty understanding parametrized nested queries that appear in the FROM
clause. Take a look at these examples:
PREPARE myStmt FROM "select * from (select ? from eeg) tmp";
(doesn't work)
PREPARE myStmt FROM "select *,? from (select * from eeg) tmp";
(works)
PREPARE myStmt FROM "select *,? from (select 'asdf') tmp";
(works)
PREPARE myStmt FROM "select * from eeg where Identifier in (select ?)";
(works)
Curious behaviour, but I can only guess that when a nested SELECT
in the FROM
clause has parameters, MySQL is missing clues in order to prepare the statement.
As for my suggestion, if I understand what you are trying to do, you don't need a parameter in the nested select. You could move it outside and hardcode a constant in the nested select, for the sake of FROM
. The following code
if ($usertest = $datasqli->prepare("INSERT INTO eeg (Identifier)
SELECT ? from (select 1) tmp WHERE ? NOT IN
(SELECT Identifier FROM eeg WHERE Identifier = ?)")) {
...should do the trick.