PHP - Get affected rows in ADODB

4.7k views Asked by At

I using ADODB to create a connection to my database. I update the data in my database, there is no error. The problem is that I can't get the number of affected rows by Affected_Rows(). I tried with very simple code but it is not working. Here is my code:

$sql = "UPDATE User SET Name=N'MyName' WHERE Id=1";
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
$cs = "provider=sqloledb;"."server=localhost;database=Test;uid=Admin;pwd=123456;Max Pool Size=100";
$conn->open($cs);

//there is no error in connecting process. I can add, update, delete normally.
if($conn->Execute($sql) === false)
{
    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->ErrorMsg(), E_USER_ERROR);
}
else 
{
    echo $conn->Affected_Rows();  //<-- Error in here
}

I have read about this function in here. My code above is almost same with example here. Is there any other way to get the number of affected rows in ADODB-PHP?

2

There are 2 answers

0
Trung On BEST ANSWER

About Affected_Rows(), I don't know why it isn't working. There is another very simple way to get the number of affected rows after execute query.

$conn->Execute($sql,$affected_rows);

echo $affected_rows;

$affected_rows return from Execute function will have value equal to number of affected rows of that query.

0
user2305921 On

just for mark.

your code:

echo $conn->Affected_Rows()

is ADODB SDK methods: http://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:affected_rows

NOT a native COM("ADODB.connection") method.

but i have same problem because this method return "variant Object" so i can't get the value.

i fixed this by edit SDK source file : adodb5/drivers/adodb-ado5.inc.php #44 line:

$this->_affectedRows =new VARIANT;

to:

$this->_affectedRows = null;

then Affected_Rows() method can return a real number for me.