tags:

views:

45

answers:

3

I got on my php script

$result1 = mysql_query("insert ignore into...this....
$result2 = mysql_query("insert into...that....
$result3 = mysql_query("insert ignore...again

those all are different queries.

when first query tries to insert, and if it has duplicate record, it does not enter this.

well I dont want to run second and third query, if first query did not enter anything.

How do I prevent 2 and 3 query running if first did not enter anything? in PHP please.

Thanks

+3  A: 

Use the mysql_affected_rows function to get the number of affected rows:

$result1 = mysql_query("insert ignore into...this....");
if (mysql_affected_rows($result1) > 0) {
    $result2 = mysql_query("insert into...that....");
    $result3 = mysql_query("insert ignore...again");
}
Gumbo
Warnig: mysql_affected_rows(): supplied argument is not valid MySQL-link resource... thats what I got ;)
Ossi
A: 

I cannot try it out myself right now, but it looks like mysql_affected_rows() may be able to accomplish what you are looking for.

Anti Veeranna
+2  A: 

You should use a transaction:

mysql_query("begin");
$result1 = mysql_query("insert ignore into...this....");
if (mysql_affected_rows() > 0) {
    $result2 = mysql_query("insert into...that....");
    $result3 = mysql_query("insert ignore...again");
    mysql_query("commit");
} else {
    mysql_query("rollback");
}

EDIT: This will only work if your tables are InnoDB. MyISAM tables do not support transactions.

Asaph
Warnig: mysql_affected_rows(): supplied argument is not valid MySQL-link resource... thats what I got ;) –
Ossi
@Ossi: Ok, I saw a problem that might cause the warning you saw. I updated my answer. Please try it again and let me know if it works.
Asaph
I took begin and rollback out, sinco I dont have InnoDB, It works now... mysql_affected_rows() works and mysql_affected_rows($result1) did NOT work...and since "Gumbo" did not fix his answer, I will give you the answer. :)
Ossi
@Ossi: If you removed begin and rollback, you might as well remove commit as well.
Asaph