Call a SQL Server stored procedure from PHP

1

What I want to do is call a stored procedure done in Sql Server to PHP, I checked the PHP manual in which the SQLSRV is used, I was guided by that manual but I get an error and the truth is not much and I want you to help me.

Then my php code:

<?php


include("conexion_sis.php");

      ini_set('date.Timezone','America/Lima');
      $time=date('H:i:s',time());                                                           
      $time2=date('Y-m-d',time());

      $codigo=$_POST['codigo'];
      $unidad_medida=$_POST['unidad_medida'];                                                            
      $descripcion=$_POST['descripcion'];  

      $precioUnitario=$_POST['precioUnitario'];

      $myparams['CodigoArticulo'] = $codigo;
      $myparams['Descripcion'] = $descripcion;
      $myparams['UnidadMedida'] = $unidad_medida;
      $myparams['TipoExistencia'] = 0;
      $myparams['FechaVigencia'] =$time2;
      $myparams['EmpId'] = 3;
      $myparams['NumDocto'] = 3;
      $myparams['Item'] = 3;
      $myparams['PrecioUnitario'] = $precioUnitario;

      $procedure_params = array(
            array(&$myparams['CodigoArticulo'], SQLSRV_PARAM_OUT),
            array(&$myparams['Descripcion'], SQLSRV_PARAM_OUT),
            array(&$myparams['UnidadMedida'], SQLSRV_PARAM_OUT),
            array(&$myparams['TipoExistencia'], SQLSRV_PARAM_OUT),
            array(&$myparams['FechaVigencia'], SQLSRV_PARAM_OUT),
            array(&$myparams['EmpId'], SQLSRV_PARAM_OUT),
            array(&$myparams['NumDocto'], SQLSRV_PARAM_OUT),
            array(&$myparams['Item'], SQLSRV_PARAM_OUT),
            array(&$myparams['PrecioUnitario'], SQLSRV_PARAM_OUT)

            );


      $sql = "exec Usp_Usp_Insertar_Productos @CodigoArticulo=?,@Descripcion=?,@UnidadMedida=?,TipoExistencia=?,FechaVigencia=?,@EmpId=?,@NumDocto=?,@Item=?,@PrecioUnitario=?";
      $stmt = sqlsrv_prepare($con, $sql, $procedure_params);

      if( !$stmt ) {
            die( print_r( sqlsrv_errors(), true));
            }

            if(sqlsrv_execute($stmt)){
              while($res = sqlsrv_next_result($stmt)){
                // make sure all result sets are stepped through, since the output params may not be set until this happens
              }
              // Output params are now set,
              print_r($params);
              print_r($myparams);
            }else{
              die( print_r( sqlsrv_errors(), true));
            }
?>

My error:

 1Array
(
    [0] => Array
        (
            [0] => 42000
            [SQLSTATE] => 42000
            [1] => 102
            [code] => 102
            [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Sintaxis incorrecta cerca de '='.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Sintaxis incorrecta cerca de '='.
        )

    [1] => Array
        (
            [0] => 42000
            [SQLSTATE] => 42000
            [1] => 8180
            [code] => 8180
            [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]No se puede preparar la instrucción o instrucciones.
            [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]No se puede preparar la instrucción o instrucciones.
        )

)
    
asked by Jhona JM 05.11.2018 в 23:58
source

1 answer

1

The error message indicates that there is a error near of a = sign. SQL Server error messages are usually so, so you have to train to interpret them.

Reading your sentence, I realized that you have 2 parameters without the sign @ , which are TipoExistencia, and 'FechaVigencia' and therefore it is the error, with this should be resolved:

$sql = "exec Usp_Usp_Insertar_Productos @CodigoArticulo=?,@Descripcion=?,@UnidadMedida=?,@TipoExistencia=?,@FechaVigencia=?,@EmpId=?,@NumDocto=?,@Item=?,@PrecioUnitario=?";
    
answered by 07.11.2018 / 17:24
source