On 28-Jul-2001 Philip Mak wrote:
> I have a table called "stats" with:
> user varchar(40) not null primary key,
> count mediumint not null.
> 
> I want to do something like this:
> 
> UPDATE stats SET count=count+1 WHERE user='$user';
> 
> But if there is no row in the "stats" table having user='$user', that
> statement will not create the row.
> 

<snip>

> 
> The following will raise an error if the user already exists, causing the
> program to stop:
> 
> INSERT INTO stats SET user='$user', count=0;
> UPDATE stats SET count=count+1 WHERE user='$user';
> 

You're on the right track:

INSERT IGNORE INTO stats (user,count) values('$user',0);
UPDATE stats SET count=count+1 WHERE user='$user';

But for something less intensive:

UPDATE stats SET count=count+1 WHERE user='$user'
if (0 == $sth->affectedrows) {
      INSERT INTO stats (user,count) values('$user',1)
}

Regards,
-- 
Don Read                                       [EMAIL PROTECTED]
-- It's always darkest before the dawn. So if you are going to 
   steal the neighbor's newspaper, that's the time to do it.
                            (53kr33t w0rdz: sql table query)

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to