سؤال

I try to use sqlsrv_query to get data from microsoft database. this is my code:

$serverName = "mssql.somee.com\sqlexpress"; //serverName\instanceName


$connectionInfo = array("UID"=>"id","PWD"=>"password","Database"=>"db");

$conn = sqlsrv_connect( "db.mssql.somee.com", $connectionInfo);

if( $conn ) {
 echo "Connection established.<br />";
}else{
 echo "Connection could not be established.<br />";
 die( print_r( sqlsrv_errors(), true));
}



$sql = "select UserName,Email from HTS_User where UserID = 7";

$stmt = sqlsrv_query($conn,$sql);//query is working
if($stmt === false){
die(print_r(sqlsrv_errors(),true));
}else{
echo "sqlsrv_query is working";
echo "<br />";
}

if(sqlsrv_fetch($stmt) ===false){
echo "fetch error";
die(print_r(sqlsrv_errors(),true));
}else{
echo "sqlsrv_fetch is working";
echo "<br />";
}

$n = sqlsrv_get_field($stmt,0);
echo "$n: ";
echo "<br />"

$e = sqlsrv_get_field($stmt,1);
echo "$e: ";

But the output is this instead of the real data:

Resource id #4: Resource id #5:

Anyone knows the problem? Thanks very much Jason

هل كانت مفيدة؟

المحلول

Try

if(sqlsrv_fetch_array($stmt) ===false){

Instead of

if(sqlsrv_fetch($stmt) ===false){

Ref:

http://php.net/manual/en/function.sqlsrv-fetch-array.php

http://www.php.net/manual/en/function.sqlsrv-fetch.php

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top