Originally Posted by Dormilich
or you just test whether the query suceeded. mysql_query() returns false on any DB/query error (like such a constraint violation).
You have to be a bit careful using that technique because it doesn't always return false. You can select rows based on certain criteria and if none are returned it doesn't return false. Likewise if you run an update that affects 0 rows it doesn't return false. I've found that false is generally returned with badly formed SQL being executed but not often for much else (although it does happen but rarely imo).
It's always better to go for the rows affected imo as that is the most accurate way of knowing that something was actually done