2

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.

Community
  • 1
  • 1
bitkot
  • 4,466
  • 2
  • 28
  • 39

1 Answers1

2

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

boobl
  • 225
  • 1
  • 5
  • 1
    I will try it. I am doing this because. I don't want to check if the combination does not exist and then try to insert. I want to let mysql do the work. – bitkot Jul 06 '14 at 14:54
  • 1
    In this case, since you're using MySQL, you can do INSERT IGNORE to suppress the warning on MySQL side – boobl Jul 06 '14 at 15:03
  • I will do that. That is a better solution that adding @. Thanks Again. – bitkot Jul 06 '14 at 15:05
  • I don't need to wrap the code try-catch then, right? – bitkot Jul 06 '14 at 15:11
  • @AmitChotaliya right, no exceptions will be thrown. MySQL will simply do nothing, if the row already exists – boobl Jul 06 '14 at 15:14
  • BTW. I still had to add @ because. IGNORE just suppresses error but it generates warning. – bitkot Jul 06 '14 at 15:34