Forum Moderators: open

Message Too Old, No Replies

MYSQL check for existing row

Chan you let mysql check for an existing row?

         

Twisted Mind

2:38 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



Can you let mysql check if a row exists in a table?
like:

Alter table `table`, if not exists `row` add `row`

greigmc

11:16 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



The type of query you're looking for is an insert query. MySQL allows you to add the keyword ignore to the insert query so that it won't flag a warning if an error occurs in the statement.

Your table will need a primary key or unique index for this to work.

So your statement will look like

insert ignore into tablename values (fields);

At the end of the statement your table will definitely have that row in it. If you need to find out if a row was actually inserted you can pull in the affected row count in the usual way and test it for being >0.

Twisted Mind

8:59 am on Jan 16, 2006 (gmt 0)

10+ Year Member



ty so if i put this on the top of a php script it would not slow it down pretty hard rite?

Twisted Mind

9:10 am on Jan 16, 2006 (gmt 0)

10+ Year Member



Doesnt work..

Twisted Mind

9:13 am on Jan 16, 2006 (gmt 0)

10+ Year Member



Error Code : 1136
Column count doesn't match value count at row 1
(16 ms taken)

is what i get trying to execute it in mysql

With statement
insert ignore into mos_pshop_user_info values (prjname)