Link to home
Start Free TrialLog in
Avatar of tim_carter
tim_carter

asked on

Why is query sometimes not executing

Hi guys

I have this query

$update = "
            UPDATE Users t1 SET
                NewMail =
                    CASE
                        WHEN (NewMail=0 OR NewMail IS NULL) THEN 1
                        ELSE NewMail + 1
                    END
            WHERE
                UserId = $rUserId
            LIMIT 1
        ";
        mysql_query($update);

it is executed with other queries..

IT DOES NOT FAIL.

But sometimes it does not update and the NewMail keeps beeing 0. why is that?
Avatar of Aaron Shilo
Aaron Shilo
Flag of Israel image

hi

probably becuse you use the LIMIT 1
this means update the last value

change the value to a larger number or remove.
Avatar of tim_carter
tim_carter

ASKER

It is only 1 user that it should update at this point? why would i run through every record?
just to be clear you need to update only one record at a time ?
YES. it is only one user..

like UserId=2


that isent what i asked.

when you use Userid= 2 will you allways retrive only one row from the table?
and that is what i need. but sometimes it does not update the NewMail value
hi
when you run this query

select * from Users
                    where NewMail=0 OR NewMail IS NULL
                     and UserId = $rUserId

notice i removed the LIMIT 1

how meny rows come bac ?
what has this query to do with what i want?

1st of the query you have done here will return almost anything because you have not encapsulated (newmail)

MY STATEMENT should update 1 user. and if the user has the value 0 or NULL in newmail it should set the value to 1, else it should add to the current value,.
>>But sometimes it does not update and the NewMail keeps beeing 0. why is that?
Are you sure that $rUserId is not empty during this time? May be query is failing due to some eorr or $rUserId has incorrect value. Use mysql_error() to detec the error and print the query to debug the issue in this way.

mysql_query($update) or die("Error in Query: $update - ". mysql_error());

Open in new window

wasifg

I am already doing that in the script..

It does not fail. And it is randomly it happens.
Can it have something to do with CASE?
ASKER CERTIFIED SOLUTION
Avatar of tim_carter
tim_carter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
less than zero. My own error