I am trying to extract data from procedure stored on a remote MSSQL server. PHP documentation says in sqlsrv_fetch($stmt)
, $stmt
refers to the resource returned by the sqlsrv_query()
or sqlsrv_execute()
.
Here is my code snippet ->
$sql = "exec USP_ABC_ABC_REPORT 'Apple','06/01/2014','10/29/2014','H','A'";
$segment ='apple';
$start_date='06/01/2014';
$end_date ='10/29/2014';
$something2 ='H';
$something3 ='A';
$abc = sqlsrv_prepare($conn, $sql, array( &$segment, &$start_date, &$end_date, &$something2, &$something3));
if( $abcd = sqlsrv_execute( $abc)){
if(sqlsrv_fetch($abcd)){
$id = sqlsrv_get_field( $abcd, 0);
echo $id;
} else{
echo "fetch failed";
}
}
This snippet shows an error:
sqlsrv_fetch() expects parameter 1 to be resource, boolean given.
sqlsrv_fetch() works fine if I use sqlsrv_query() instead of sqlsrv_execute/prepare. The possible reason is that sqlsrv_query() returns mixed output instead of boolean.
Could anyone help with using sqlsrv_fetch with execute.
sqlsrv_execute
returns a boolean indicating success or failure.
If the call to sqlsrv_prepare
is successful, it returns a statement resource. So you can just pass $abc
as a parameter of sqlsrv_fetch
and sqlsrv_get_field
:
if (sqlsrv_fetch($abc)){
$id = sqlsrv_get_field($abc, 0);
echo $id;
}
Your insights about the cause are correct. Check the documentation for sqlsrv_execute and each of the functions.
sqlsrv_execute returns either true or false , ( boolean ). And this is not what fetch is expecting as input.
Once you undestand the need of matching types of expected parameters on each function you will be able to get along easier with any sort of code.
while into a develpment environment, try to
var_dump($abcd);
just after assigning to see its value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.