Search code examples
phpmysqladodb-php

How to find affected rows in UPDATE query using Adodb library PHP?


I am using Adodb PHP library to handle db functions. Most of the functionality has been handled using Stored Procedures. However, in few cases, I need to write quick custom queries to complete the flow.

Code:

$Result = array();
$this->DB->SetFetchMode(ADODB_FETCH_ASSOC);
$Result = $this->DB->PrepareSP("UPDATE Member SET FirstName = '" . $param['FirstName'] . "', LastName = '" . $param['LastName'] . "', Email = '".$param['Email']."', DateOfBirth = '".$param['DateOfBirth']."', HoroscopeID = ".$param['HoroscopeID'].", Gender = '".$param['Gender']."', CountryID = ".$param['CountryID']." WHERE ID = ".$param['MemberId'].";SELECT @@IDENTITY AS AffectedRows;");
        $Result = $this->DB->GetArray($Result);

Result:

array (size=1)
  0 => 
    array (size=1)
      'AffectedRows' => null

I have also tried:

$this->DB->affected_rows();

It returns 0 every time. I have already echo the inside query and run on navicat, it works fine. However, when its called through code, its not updating any records.


Solution

  • Here is how to access stored procedure output data in ADOdb:

    /*
    * Your stored procedure preparation returns a handle
    */
    $procedure = $this->DB->prepareSp("your statement...");
    
    /*
    * You prepare and set an output parameter to receive your affected rows
    * The name of the parameter matches the stored procedure variable
    */
    $outParameterName  = 'AffectedRows';
    $outParameterValue = 0;
    
    $ok = $this->DB->outParameter($procedure,$outParameterName,$outParameterValue);
    
    /*
    * Execute the procedure handle , it is an update so a resultset is not returned
    */
    $result = $this->DB->execute($procedure);
    
    /*
    * The value of the Affected rows is now available in your out Parameter value
    */ 
    print "AffectedRows IS NOW $outParameterValue";