DS9
DS9

Reputation: 3033

mysql_affected_rows is not giving desired result in foreach loop

foreach($chk as $key=>$invoiceno)
{
  $QryUp = mysql_query("update `tbl1` set `paid` = 'y' where `invoice_no`='".$invoiceno."' and `uniquekey`='".$_SESSION['uniquekey']."'") or die(mysql_error());
  $id=mysql_affected_rows();

 //insert data into tbl_school_account
 $QryIns=mysql_query("insert into `tbl2` (`type`, `ref_id`) values('d', '$id')");
}

here $id return same value.
exa:when this loop is work it update tbl1 table.their id is 1 and 2.it update perfect. but mysql_affected_rows return only 1 (two time).

so where's the problem?

and i am aware about deprecated function.

Upvotes: 0

Views: 212

Answers (2)

martijnve
martijnve

Reputation: 983

You want this function instead of affected_rows:

http://php.net/manual/en/function.mysql-insert-id.php

"Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT)."

Upvotes: 1

slugonamission
slugonamission

Reputation: 9642

The mysql_affected_rows call returns the number of affected rows, not the PK of affected rows. You're going to have to run a SELECT using the invoice ID etc to get the information that you appear to want.

Upvotes: 1

Related Questions