Fatal error when generating excel with PHPExcel and SQL ... "Call to a member function query () on a non-object .."

1

My problem in particular is that I must generate an excel [.xlsx] (use PHPExcel) with data from SQL Server with PHP.

The script that I edited already existed with MySQL statements and it worked perfectly, but when I change the sentences by SQL it generates the following error:

  

"Fatal error: Call to a member function query () on a non-object in C: \ ..."

And the code in summary is:

$query_Data="select * from etc etc etc";

$connectionInfo1 = array( "Database"=>xxx, "UID"=>yyy, "PWD"=>zzz);
$conexion = sqlsrv_connect($serverName, $connectionInfo1);

$resultado = $conexion->query($query_Data); [el error que funcionaba con mysql]
(cualquier sentencia que reemplace a query() me genera error)

Note: I do not know if I have to see that the method of connection call is different in MySQL (that if it works) to SQL (which does not work.)

I appreciate you can help me with information on how I can solve this "fatal error", thank you in advance!

    
asked by KrissDrakon 13.04.2018 в 15:53
source

1 answer

0

The problem you have is that the function you call sql_srv_connect is giving a connection error, if you read the documentation ( link ) returns false when there is a problem, that's why it gives that function error not found.

To know the error you have to call sqlsrv_errors (), this gives you the problem.

something like this:

$conexion = sqlsrv_connect($serverName, $connectionInfo1);

if( $conexion ) {
     // hace la consulta
}else{
     echo "Conexión no se pudo establecer.<br />";
     die( print_r( sqlsrv_errors(), true));
}
    
answered by 13.04.2018 в 22:26