I have an IF NOT EXISTS statement. IF it doesn’t exist an INSERT statement follows. IF EXISTS I need to understand what is/can be returned so that I can interpret it with PHP.
so:
IF NOT EXISTS (SELECT id FROM users WHERE lname='SMITH')
INSERT INTO users (lname='SMITH')
I thought that the negative would return no result so could use a standard
if ($result){echo "record added";}
else {echo "duplicate - no record added";}
But that’s not what’s happening and I’m not sure how to achieve what I need – any suggestions?
Thank you for your kind attention.
In both cases
$resultwould return the response for a successfully run command and nothing more.INSERTdoesn’t yield any rows, and neither does anIFstatement that doesn’t have a matching condition.You can use
SELECT SCOPE_IDENTITY()if you want to retrieve the inserted ID, and then you can check if$resulthas returned a row.