mssql_connect和sqlsrv_connect之间的区别

时间:2011-06-09 17:54:24

标签: php sql-server

我刚刚从mssql_connect更改了连接驱动程序(扩展程序)以使用sqlsrv_connect。不幸的是,事情似乎并不像我想的那样有效。如果有人能告诉我什么是错的,我会很感激,我该如何解决它。

代码摘录:

//a oracle DB moudle with a generic functions such as 
//db_connect() db_query()

/// turn on verbose error reporting (15) to see all warnings and errors 
error_reporting(15);
//generic DB operations

// Global record . for using in 
$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");
    //echo "DEBUG: hdb=$dbh"; 
}

//connect to given database 
//return handler to DB


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;

}

//close DB
function dbo_logoff($dbh)
{
    if(!db_is_connected($dbh)) echo("DB disconnect error");
    sqlsrv_close($dbh);
}

以下是使用mssql_connect的原始代码:

//a oracle DB module with a generic function such as 
//db_connect() db_query()

/// turn on verbose error reporting (15) to see all warnings and errors 
error_reporting(15);
//generic DB operations

// Global record . for using in 
$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");
    //echo "DEBUG: hdb=$dbh"; 
}

//connect to given database 
//return handler to DB
function dbo_logon($dbserver,$dbuser,$dbpass,$dbname)
{
    // $dbh=dbok(mssql_pconnect($dbserver,$dbuser,$dbpass));
    $dbh=dbok(mssql_connect($dbserver,$dbuser,$dbpass));
    //error_log("connect to [$dbname]".date("His")." \r\n",3,"/tmpbull.log"); //DBEUG DELME
    dbok(mssql_select_db($dbname,$dbh));
    //dbo_exec($dbh,"alter session set NLS_DATE_FORMAT='dd-mm-yyyy //hh24:mi:ss'");
    return $dbh;

}

//close DB
function dbo_logoff($dbh)
{
    if(!db_is_connected($dbh)) echo("DB disconnect error");
    mssql_close($dbh);
}

请注意,我必须将身份验证方法从SQL身份验证更改为Windows身份验证,因为sqlsrv_connect使用Windows身份验证而不是SQL身份验证。是吗?

2 个答案:

答案 0 :(得分:2)

我认为这两个链接有助于理解 mssql和sqlsrv之间的区别

虽然简而言之,引用其中一篇文章:

  

sqlsrv 驱动程序由Microsoft`构建,维护和支持

  

mssql 驱动程序是一个社区构建的驱动程序。

     

我不确定这个驱动程序最近是如何更新或维护为官方PHP扩展,但是从PHP 5.3的发布版本开始,PECL不再提供它。一个快速的互联网搜索出现了几个地方下载mssql驱动程序,但我发现它们都没有表明驱动程序正在积极维护。

来源:http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx

至于您的代码示例,请参阅以下内容:

$dbserver = "computername\SQLEXPRESS";

$dbname = "BULL";   
$connetion = dbo_logon($dbserver,$dbname);  
function dbo_logon($dbserver,$dbname) {  
    $connectionOptions = array("Database"=>$dbname);  
    $dbh=sqlsrv_connect($dbserver, $connectionOptions);  
    if(!$dbh){  
         die("Error in Database connection");  
         return false;   
    }  
 return $dbh;   
 }

我认为这适用于连接。请注意代码未经过测试。

答案 1 :(得分:0)

" sqlsrv驱动程序由Microsoft`"构建,维护和支持。好吧现在2014年9月,没有官方发布支持php 5.6上一次正式发布是从2012年4月开始.MS风格......