I have very strange error with insert query to MySQL throught PDO.
I want insert record in table if this record doesnt exists in this table yet.
$query = "INSERT INTO Phrases (KeyText)
SELECT * FROM (SELECT :key_text) as tmp WHERE NOT EXISTS (SELECT 1 FROM Phrases WHERE KeyText = :key_text)";
try
{
$preparedStatement = $db->prepare($query);
foreach ($phrases as $phrase)
{
$preparedStatement->execute(array(':key_text' => $phrase));
echo "-";
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
It throws exeption: Call to a member function execute() on a non-object.
I dont understand where is my error in SQL command. Other pieces of code is correct because when I changed SQL command to other (for example: SELECT :key_text as kt) it works correctly.
Thanks for answers.
UPDATE:
The error is:
[0] => HY000
[1] => 1096
[2] => No tables used
How can I make correct SQL command for MySQL?
Variant with
INSERT INTO Phrases (KeyText)
SELECT :key_text WHERE NOT EXISTS (SELECT 1 FROM Phrases WHERE KeyText = :key_text)
doesnt work in MySQL too.
UPDATE 2:
INSERT INTO Phrases (KeyText)
SELECT :key_text FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM Phrases WHERE KeyText = :key_text)
This query with DUAL table works!
Your prepare call is failing, and you’ve simply assumed it succeeded.
Try
to catch the error.