Link to home
Start Free TrialLog in
Avatar of Gary
GaryFlag for Ireland

asked on

MySQL INSERT IGNORE - Check if insert happened

How can I check if the insert happened when using IGNORE - can it be done in one statement?
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

<SQL Server answer, may not be MySQL answer>

Use @@ROWCOUNT in the statement immediately after the INSERT>

INSERT INTO Table1 (goo, foo, boo)
SELECT gooey, fooey, booey
FROM Table2

SELECT 'You just inserted ' + CAST(@@ROWCOUNT as varchar(max)) + ' rows.'
Avatar of Gary

ASKER

Its SELECT ROW_COUNT();
Which runs fine when I execute the resulting sql directly in MySQL but from PHP I get
check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT ROW_COUNT(); INSERT INTO
There is a insert statement before the SELECT ROW_COUNT()
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>there are two inserts in the one statement.
Please copy-paste that statement into this question.

Also, I recommend removing VB.NET, and adding PHP zone to this question.
Not every MySQL dude will PHP.
Avatar of Gary

ASKER

Well it's working will just leave it open a little while longer so someone maybe able to explain it to me.
Never added .net to the zones...
In my experience, this function returns values that make sense.  I think it would be worth testing to see if it gives you what you want.
http://php.net/manual/en/function.mysql-affected-rows.php
Avatar of Gary

ASKER

The PDO rowcount is what I'm using.
Just curious why the row count only returns the number of rows (either 1 or 0) for the first statment (which might not insert) and not for the second insert (which always inserts)
How its working now is what I want.
Just curious why it doesnt count the rows for the second insert - is this a reliable method for getting the row count.
Hi,

Could you post your code showing how you are using pdo to do the inserts and get the rowcount? It would probably help in determining what is going on.
Avatar of Gary

ASKER

$del = $pdo->prepare(
"INSERT IGNORE INTO table1 (col1,col2) values ($col1,$col2);
INSERT INTO table2 (col3,col4) values ($col3,$col4);"
);

$del->execute();

$count = $del->rowCount();

Open in new window

Avatar of Gary

ASKER

Solved myself