bitkot
bitkot

Reputation: 4504

Hide php mysql constraint violation warning for selected functions

I am new to PHP and I am developing a project using http://www.php-mvc.net/ light php MVC framework. I have a many to many mapping between two tables i.e

collection : id
profile : id
collection_profile : collection_id <--> profile_id

I have created a function where I insert mapping row in the collection_profile table. Now the problem. I have create composite key of collection_id and profile_id so it wont let me enter duplication combination, that is what i want.

I have added these lines in try,catch block. but It still shows Warning: PDOStatement::execute(): SQLSTATE[23000]: Integrity constraint violation on the page.

I found error_reporting(E_ERROR) but I am not able to figure out where to put this line. because I want to disable warning only for that function.

BTW, I am doing this because. I don't want to check if the row exist and then try to insert. I want mysql to do the work.

Let me know if you require anymore information.

Thanks in advance.

Upvotes: 2

Views: 249

Answers (1)

boobl
boobl

Reputation: 225

In PHP, typing @ before the statement lets you suppress any warnings it produces

@$stm->execute();

But still it would be much better if you understood why exactly the warning is issued. You're probably doing something wrong.

Edit: You can suppress this warning on MySQL side by executing INSERT IGNORE instead of INSERT, see http://dev.mysql.com/doc/refman/5.6/en/insert.html

Upvotes: 2

Related Questions