duanpu1111 2011-06-09 17:54
浏览 312

mssql_connect和sqlsrv_connect之间的区别

I have just changed the connection driver (extension) from mssql_connect to work with sqlsrv_connect. Unfortunately things do not seem to work as I wanted. I'd appreciate if someone can tell me what’s wrong and how can I fix it.

Code excerpt:

//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"."
";
        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);
}

Here is the original code using 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"."
";
        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")." 
",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);
}

Note that I had to change the authentication method from SQL authentication to Windows authentication because sqlsrv_connect uses Windows authentication instead of SQL authentication. Is that right?

  • 写回答

2条回答 默认 最新

  • 普通网友 2011-06-09 19:14
    关注

    I think these two links will help to understand Difference between mssql and sqlsrv

    Though in short, to quote one of the articles:

    The sqlsrv driver is built, maintained, and supported by Microsoft`

    and

    The mssql driver is a community-built driver.

    I’m not sure how recently this driver was updated or maintained as an official PHP extension, but as of the release of PHP 5.3, it is no longer available with PECL. A quick internet search turns up a few places to download the mssql driver, but none of them that I’ve found indicate that the driver is being actively maintained.

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

    As for your code sample, see below:

    $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;   
     }
    

    I think this will work for the connection.please note the code is not tested.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀