2015-01-16 22:07 阅读 66

如果INSERT IGNORE回显“查询被忽略”? (MySQL的/ PHP)

I have a query like this:

$query="INSERT IGNORE INTO mytable (key, word1, word2) VALUES (1, 'abc', 'def')";

And I'd like to somehow "catch" if the query was ignored because the primary key already exists.

Is there any was to make an "if" statement so my program could notify the user that the query was ignored?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douyan1453 douyan1453 2015-01-16 22:24

    I solved it by using mysql_affected_rows to check if no rows were affected by the query.

        echo "Success.";    
        echo mysql_affected_rows($conn); //returns 1
        echo "Some error." . mysql_error(). " ".mysql_affected_rows($conn); //returns 0 affected rows since it was ignored
    点赞 评论 复制链接分享
  • dongzaizai2015 dongzaizai2015 2015-01-16 22:19

    The problem is when you especially ignore the errors then you don't get them. So you have to remove the IGNORE and catch the error instead.

    You don't get any errors in your case.

    点赞 评论 复制链接分享