|
Posted by David Haynes on 10/29/61 11:43
Treefrog wrote:
> Hi all,
>
> I came accross a little piece of code today that I thought was quite
> cute, but I'm not sure if it's any better than my usual method. The
> code ultimately needs to see if a unique identifier exists in a
> database, then either insert or update accordingly.
>
> The way I've always done it is:
>
> SELECT * FROM blah where.....
>
> if (there's a row) then
> UPDATE.....
> else
> INSERT
>
> but the nifty bit of code did it like this.
>
> UPDATE....
> if (mysql_affected_rows == 0) then
> INSERT
>
> Which I presume if more efficient, but I'd like other people opinions
> please.
>
> Cheers,
>
> Treefrog
>
You would probably be better changing the test to be
if( mysql_affected_rows() != 1 )
I have seen situations where the update has failed and affected rows
returns -1, not 0.
-david-
[Back to original message]
|