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?