问题描述
我正在尝试使用PDO连接到SQL Server数据库.我一直在努力挣扎,现在我在尝试连接到SQL Server的页面上收到此致命错误
I am trying to use PDO to connect to a SQL Server database. I have been struggling with it for a little bit now I get this Fatal error on my page where I am trying to connect to SQL Server
我从以下位置下载了驱动器 http://www.microsoft.com/zh-cn/下载/details.aspx?id=20098 (SQLSRV30.EXE)我已将这些驱动程序放置在Server 2008 R2服务器上安装了php的ext目录中.
I have downloaded the drives fromhttp://www.microsoft.com/en-us/download/details.aspx?id=20098 (SQLSRV30.EXE)I have placed those drivers in the ext directory where php is installed on the Server 2008 R2 server.
我还在php.ini文件的扩展列表的末尾添加了这两行
I also have added these 2 lines at the end of the extension list in the php.ini file
extension=php_pdo_sqlsrv_53_nts.dll
extension=php_sqlsrv_53_nts.dll
我正在使用PH PVersion 5.3.19服务器API CGI/FastCGI线程安全性已禁用
I am using PH PVersion 5.3.19Server API CGI/FastCGIThread Safety disabled
我确实看到了pdo_sqlsrv
,但是我没有看到客户端API版本.
I do see pdo_sqlsrv
but I don't see a Client API Version.
要使用PDO连接到具有SQL数据库的远程服务器,我还需要做什么?我需要在远程服务器上安装任何东西吗?
What else do I need to do to be able to use PDO to connect to the remote server that has SQL databses? Do I need to install anything on the remote servers?
这是我的php我的管理员部分的屏幕截图
this is a screenshot of my php my admin section
这是我的连接课程
<?php
class connection {
private $connString;
private $userName;
private $passCode;
private $server;
private $pdo;
private $errorMessage;
protected $lastQueryTime;
protected $lastQuery;
private $pdo_opt = array (
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
);
function __construct($dbName = DATABASE_NAME, $serverName = DATABASE_HOST){
//sets credentials
$this->setConnectionCredentials($dbName, $serverName);
//start the connect
$this->startConnection();
}
function startConnection(){
$this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);
if( ! $this->pdo){
$this->errorMessage = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
$this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
echo $this->getError();
}
}
//this will close the PDO connection
public function endConnection(){
$this->pdo = null;
}
//return a dataset with the results
public function getDataSet($query, $data = NULL)
{
$start = microtime(true);
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
$ret = $cmd->fetchAll();
//$cmd->closeCursor();
$this->lastQueryTime = microtime(true) - $start;
$this->lastQuery = $query;
return $ret;
}
public function processQuery($query, $data = NULL)
{
$start = microtime(true);
//$this->pdo->beginTransaction();
$cmd = $this->pdo->prepare( $query );
$ret = $cmd->execute($data);
//$this->pdo->commit();
//$cmd->closeCursor();
$this->lastQueryTime = microtime(true) - $start;
$this->lastQuery = $query;
return $ret;
}
//return last insert id
public function lastInsertId($name = NULL) {
if(!$this->pdo) {
return false;
}
return $this->pdo->lastInsertId($name);
}
public function getOneResult($query, $data = NULL){
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
return $cmd->fetchColumn();
}
public function getError(){
if($this->errorMessage != '')
return $this->errorMessage;
else
return true; //no errors found
}
//this where you need to set new server credentials with a new case statment
function setConnectionCredentials($dbName, $serv){
switch($serv){
//MS SQL server
case 'SQLSERVER':
$this->connString = 'sqlsrv:server='.$serv.';database='.$dbName;
$this->userName = 'username';
$this->passCode = 'password';
break;
//the defaults are predefined in the APP_configuration file - DO NOT CHANGE THE DEFAULT
default:
$this->connString = 'mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME.';charset=utf8';
$this->userName = DATABASE_USERNAME;
$this->passCode = DATABASE_PASSWORD;
break;
}
}
public function lastQueryTime() {
if(!$this->lastQueryTime) {
throw new Exception('no query has been executed yet');
}
return $this->lastQueryTime;
}
public function lastQuery() {
if(!$this->lastQuery) {
throw new Exception('no query has been executed yet');
}
return $this->lastQuery;
}
}
?>
这就是我使用我的班级提取数据集的方式
This is how I use my class to pull a dataset
<?php
include('connection.php');
$sql_db = new connection('databaseName','SQLSERVER');
$call_details = $sql_db->getDataSet('SELECT
LocalUserId AS loginName,
RemoteNumberCallId AS PhoneNumber,
SUM(CallDurationSeconds + HoldDurationSeconds + LineDurationSeconds) AS totalTalk
FROM dbo.CallDetail WHERE LocalUserId = \'blah\' AND RemoteNumberCallId = \'123456789\'
GROUP BY LocalUserId, RemoteNumberCallId');
$call_details->endConnection();
?>
我什至尝试了这段代码,所以我不会使用我的课程,并且仍然会遇到相同的错误
I have even tried this code so I won't use my class and I still get the same error
$ss = new PDO("sqlsrv:server=SQLSERVER; Database=databaseName", "userName", "Password");
推荐答案
我已替换
extension=php_sqlsrv_53_nts.dll
extension=php_pdo_sqlsrv_53_nts.dll
使用
extension=php_pdo_sqlsrv_53_nts_vc9.dll
extension=php_sqlsrv_53_nts_vc9.dll
我打算使用SQLSRV20.EXE来使用SQLSRV30.EXE.这有效.
Insted on using SQLSRV30.EXE I used the SQLSRV20.EXE This worked.
@FreshPrinceOfSo感谢您的帮助:)
@FreshPrinceOfSo Thanks for your help :)
谢谢:)
这篇关于如何在Windows 2008 Server 2008 R2上安装pdo_sqlsrv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!