Gary
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?
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()
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>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.
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.
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...
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
http://php.net/manual/en/function.mysql-affected-rows.php
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.
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.
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.
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();
ASKER
Solved myself
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.'