I just changed the connection driver (extension) from mssql_connectto work with sqlsrv_connect. Unfortunately, everything does not work the way I wanted. I would appreciate it if someone would tell me what happened and how I can fix it.
Code Excerpt:
error_reporting(15);
$curr_rec = NULL;
function dbok($res)
{
if($res==false){
echo "DB error: "."FIXME need error mesg"."\n";
exit;
}
return $res;
}
function db_now_expr()
{
return "getdate()";
}
function db_is_connected($dbh)
{
return $dbh>0;
}
function ensure_connected($dbh)
{
if(!db_is_connected($dbh)) die("DB is not connected, operation failed");
}
function dbo_logon($dbserver,$dbname)
{
$tsql = ??????? ====>>> what should i put here????
$dbserver = "computername\SQLEXPRESS";
$dbname = MY_Database_name;
$connectionOptions = array("Database"=>"BULL");
$dbh=dbok(sqlsrv_connect($dbserver, $connectionOptions));
dbok(sqlsrv_query($dbname,$dbh));
$stmt = sqlsrv_query( $connectionOptions, $tsql );
return $dbh;
}
function dbo_logoff($dbh)
{
if(!db_is_connected($dbh)) echo("DB disconnect error");
sqlsrv_close($dbh);
}
Here is the source code using mssql_connect:
error_reporting(15);
$curr_rec = NULL;
function dbok($res)
{
if($res==false){
echo "DB error: "."FIXME need error mesg"."\n";
exit;
}
return $res;
}
function db_now_expr()
{
return "getdate()";
}
function db_is_connected($dbh)
{
return $dbh>0;
}
function ensure_connected($dbh)
{
if(!db_is_connected($dbh)) die("DB is not connected, operation failed");
}
function dbo_logon($dbserver,$dbuser,$dbpass,$dbname)
{
$dbh=dbok(mssql_connect($dbserver,$dbuser,$dbpass));
dbok(mssql_select_db($dbname,$dbh));
return $dbh;
}
function dbo_logoff($dbh)
{
if(!db_is_connected($dbh)) echo("DB disconnect error");
mssql_close($dbh);
}
Note that I had to change the authentication method from SQL authentication to Windows authentication, as it sqlsrv_connectuses Windows authentication instead of SQL authentication. Is it correct?
source
share