Problems with sqlsrv_errors in php

0

I am using PHP 5.6 on IIS 10 with SQL Server 2014, and I have the following function to make inserts, updates and deletes to the database:

<?php
public function ejecutarSQL($sql,$parametrosSQLServer=array()){
    $bandera=false;
    $msg="";
    $datos=array();
    $nowRows=0;
    $datos_pr=array();
    $cadena_replace="";
    if( $this->conexion){
        $parametros =array();
        if(count($parametrosSQLServer)>=1){
            foreach($parametrosSQLServer as $key => &$value) {
                $parametros[] = &$value;
            }
            $resultado=sqlsrv_prepare( $this->conexion,$sql,$parametros);
        }else{
            $resultado=sqlsrv_prepare( $this->conexion,$sql,$parametros);
        }
        $result= sqlsrv_execute($resultado);
        if($result != false){
            $nowRows=sqlsrv_rows_affected($resultado);
            if($nowRows ==1){
                $rst=sqlsrv_query($this->conexion,"SELECT @@IDENTITY AS ID ");
                $row =  sqlsrv_fetch_array($rst);
                if ($row["ID"] == null){
                    $datos[] =  "Registro exitoso";
                }else{
                    $datos[] =  $row["ID"];
                }
                sqlsrv_free_stmt($rst);
                $bandera=true;
                sqlsrv_free_stmt($resultado);
            }else{
                $bandera=false;
                $msg = sqlsrv_errors();//Esta linea siempre devuelve null aun con registros intencionalmente duplicados
                $nowRows=0;
                $msg=$msg[1]['message'];
                sqlsrv_free_stmt($resultado);
            }
        }else{
            $bandera= false;
            $nowRows=0;
            $msg = sqlsrv_errors();
            $msg=$msg[0]['message'];
        }
    }
    if ($bandera==true){
        return array(true, $nowRows, $datos);
    }else{
        return array(false, $nowRows, $msg);
    }
}

The code works correctly to do the operations. What does not work correctly is the sqlsrv_errors () function since when sending inserts with duplicate records, this function always returns null, and I can not see a description of the error (which I need).

I read that sqlsrv_errors () only returns errors and not warnings, so I modified the configuration so that I returned the warnings as errors, but the problem continues:

$this->datosConexion=@parse_ini_file($rutaProperties, false, INI_SCANNER_RAW);
    $this->schema=$this->datosConexion["SsqlsvrSchema"];
    $this->serverName = $this->datosConexion["SsqlsvrNombreServidor"]."\MSSQLSERVER,".$this->datosConexion["SsqlsvrPuerto"];
    $this->parametros = array( "Database"=> $this->datosConexion["SsqlsvrBaseDatos"] ,"UID"=> $this->datosConexion["SsqlsvrUsuario"] , "PWD"=>$this->datosConexion["SsqlsvrPass"], "CharacterSet" => "UTF-8");
    sqlsrv_configure('WarningsReturnAsErrors',0);//Esta es la linea que agregue
    $this->conexion = sqlsrv_connect( $this->serverName, $this->parametros);
    if($this->conexion) {
        return (true);
    }else{
         return(false);
    }

Does anyone know how to solve it? Will it be php.ini configuration?

    
asked by abrahamhs 07.03.2017 в 21:25
source

1 answer

1

The sqlsrv_errors() documentation indicates that the alerts are treated as errors but places some exceptions to the rule, which makes it confusing to understand. You could try altering the parameters of the two functions to see if any combination works for you. That is, try the following combinations:

  • sqlsrv_errors(SQLSRV_ERR_ERRORS) and sqlsrv_configure("WarningsReturnAsErrors", 0) .
  • sqlsrv_errors(SQLSRV_ERR_ERRORS) and sqlsrv_configure("WarningsReturnAsErrors", 1) .
  • sqlsrv_errors(SQLSRV_ERR_WARNINGS) and sqlsrv_configure("WarningsReturnAsErrors", 0) .
  • sqlsrv_errors(SQLSRV_ERR_WARNINGS) and sqlsrv_configure("WarningsReturnAsErrors", 1) .
  • Microsoft documentation ( How to handle errors and warnings with the SQLSRV driver ) has a very detailed example and can help you.

        
    answered by 08.03.2017 в 03:20