开发者

Limit second and third sql if no insert

I got on my php script

$result1 = mysql_query("insert ignore into...t开发者_StackOverflow社区his....
$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


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");
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜