Connecting Tech Pros Worldwide Help | Site Map

Not getting expected MySQL error

KIPAX
Guest
 
Posts: n/a
#1: Jul 17 '05


I am inserting values into a row.. works OK

If I try to insert a value into a row that doesnt exist (using where)
it always returns true the same as if it did work

I want to update rows that have one unique field.. if the row doesnt
exists it should return 0 rows updated.. I then create the row...

But it always returns 1 even when I update a row that doesnt exist..
How do I get an error or a false return?

Any help appreciated :)


Jon Kraft
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Not getting expected MySQL error


KIPAX <mail@notarealemailaddy.com> wrote:
[color=blue]
> I am inserting values into a row.. works OK
>
> If I try to insert a value into a row that doesnt exist (using where)
> it always returns true the same as if it did work[/color]

You can't use WHERE in an INSERT statement. Doesn't make sense either.
[color=blue]
> I want to update rows that have one unique field.. if the row doesnt
> exists it should return 0 rows updated.. I then create the row...
>
> But it always returns 1 even when I update a row that doesnt exist..
> How do I get an error or a false return?[/color]

Use mysql_affected_rows() after the UPDATE statement - this returns 0 if
no record was updated.

HTH;
JOn
Pedro Graca
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Not getting expected MySQL error


KIPAX wrote:[color=blue]
> I want to update rows that have one unique field.. if the row doesnt
> exists it should return 0 rows updated.. I then create the row...[/color]

you can try the (non-standard) REPLACE command
http://www.mysql.com/doc/en/REPLACE.html

It does a DELETE and a INSERT for rows that exist
and a INSERT for rows that don't already exist.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
KIPAX
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Not getting expected MySQL error


On 11 Dec 2003 11:24:30 GMT, Jon Kraft <jon@jonux.co.uk> wrote:

[color=blue][color=green]
>> I am inserting values into a row.. works OK
>>
>> If I try to insert a value into a row that doesnt exist (using where)
>> it always returns true the same as if it did work[/color]
>
>You can't use WHERE in an INSERT statement. Doesn't make sense either.[/color]

I am not.. I am trying to insert values into existing rows. But if
row doesnt exist then i was expecting it to let me know.. I would then
use a normal insert to create a new unique row.. the where was only
used to insert into an existing row.
[color=blue]
>Use mysql_affected_rows() after the UPDATE statement - this returns 0 if
>no record was updated.[/color]


Thats the ticket... Thanks :)


Closed Thread