gpt4 book ai didi

php - mssql_connect 和 sqlsrv_connect 之间的区别

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:03 24 4
gpt4 key购买 nike

我刚刚将连接驱动程序(扩展)从 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 身份验证。是吗?

最佳答案

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

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

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

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.

来源: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;
}

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

关于php - mssql_connect 和 sqlsrv_connect 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6297132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com