Avatar of Marthaj
Marthaj
Flag for United States of America

asked on 

PHP 7.4 and Stored Procedure using PDO problem

I am having two problems with converting some code from PHP 5.6 to PHP 7.4 and using PDO with a
SQL stored procedure (MS SQL Server 2014). It worked just fine in PHP 5.6. No changes have been made to the stored procedure.
I need to insert a new record with some values into a table and return the primary record id of the newly inserted record. There doesn't seem many PDO::parameters available either. I am wondering about the reliability of using PDO for stored procedures as I have read some postings concerning that matter.
It does not work nor does it stop processing. It continues with the application.
If there is an error, I want the program to halt execution.
Two of the incoming values are money - $strIdocMoney and $strFees.
And one is a datetime - $wrkFixedEmailSentDate.
It might be the type of error message in my catch section that I am displaying, but what I am using isn't very helpful at all to track down the problem as it doesn't really tell me anything. Any help with that would be appreciated too.
Below is the coding:
Function InsDocInfoRec($wrkRecvDate, $emailSendDate, $strIdocMoney, $strIDOCNbr)
{
   
// INSERTS ONE PER TEXT FILE
    $primaryid = '';
    $strRegionId = trim($_SESSION['RegionId']) ;
   
    $strSecTotal = trim($strIdocMoney);
    $strSecTotal = preg_replace('/[^0-9.-]/', '', $strSecTotal);
   
    $strSupplier = $_SESSION['GlobalSupplier'];
    $strFees = 0;
   
// NEW DATE FORMAT
$timestamp = strtotime($emailSendDate);
$wrkFixedEmailSentDate = date("Y-m-d", $timestamp);
   
ECHO '<BR><BR><BR>REGION ID: ' . $strRegionId;
ECHO '<BR>SUPPLIER: ' . $strSupplier;
ECHO '<BR>IDOCNBR: ' . $strIDOCNbr;
ECHO '<BR>VENDOR/DETAIL TOTAL: ' . $strSecTotal;
ECHO '<BR>DATE RECEIVED: ' . $wrkFixedEmailSentDate  . '<BR><BR>';


$connect = new PDO("sqlsrv:Server=$servername;Database=$dbname", $_SESSION['uid'], $_SESSION['pwd']);
    try {

            $insRec = "CALL EXEC ins_new_importIDocInfo(:RegionId,:Supplier,:IDoc,:AmtRcvd,:DateRcvd,:Fees,@RECID)";

            $stmt = $connect->prepare($insRec);
            $stmt->bindParam(':RegionId', $strRegionId, PDO::PARAM_STR); 
            $stmt->bindParam(':Supplier', $strSupplier, PDO::PARAM_STR); 
            $stmt->bindParam(':IDoc', $strIDOCNbr, PDO::PARAM_STR); 
            $stmt->bindParam(':AmtRcvd', $strSecTotal, PDO::PARAM_STR); 
            $stmt->bindParam(':DateRcvd', $wrkFixedEmailSentDate, PDO::PARAM_STR); 
            $stmt->bindParam(':Fees', $strFees,PDO::PARAM_STR);

            $stmt->execute();
            $result = $stmt->fetch();
            $primaryid = $result['@RECID'];
            var_dump($primaryid);

            $strLogMsg = $strLogMsg . 'IMPORTIDOCINFO-INSERTED: ' . strval($srtRegionId)
                      . '-' . strval($strSupplier) . '-' . strval($strIDOCNbr)
                      . '-' .  strval($strSecTotal) . '-' .strval($wrkFixedEmailSentDate)
                      . '-' .strval($strPrimaryKeyIdoc) . PHP_EOL;
            RtnWriteLogMsg($strLogMsg);

        } catch (PDOException $e) {
            echo $e->getMessage();
            $errmsg = $e->getMessage();
            echo '<BR>UNABLE TO INSERT RECORD INTO InsDocInfoRec</br>';
            echo '<BR>ERROR MSG: ' . $errmsg;
            echo '<BR>EXITING APPLICATION</br>';
            echo '<BR><BR> ** UNABLE TO INSERT DETAIL LINE INTO InsDocInfoRec **'; 
            $strLogMsg = 'SQL STATEMENT FAILED WITH ERROR: ' . $errmsg . PHP_EOL;
            RtnWriteLogMsg($strLogMsg);
            $nbr = ($nbr + 1);
            die($e);
      }
      
return $primaryid; 
}                        

Open in new window

And this is the only error I receive and then it continues processing.
C:\wamp\www\Emails\Emails-V7.php:793:null

Open in new window

Any help would be appreciated.. thank you in advance. 
PHP

Avatar of undefined
Last Comment
Marthaj

8/22/2022 - Mon