Ярлыки

.htaccess (4) тестирование (8) шаблоны проектирования (3) css (5) Debian (6) docker (2) Doctrine2 (6) Git (6) html (4) java (6) javascript (13) jquery (11) LFS (3) linux (23) mac os (4) mod_rewrite (2) MSSQL (4) MySQL (18) ORM Doctrine (17) patterns (3) PDO (3) perl (7) PHP (64) PHPUnit (8) Python (15) SEO (2) Silex (1) SimpleXML (1) SQL (14) ssh (4) Ubuntu (24) Yii1 (1) Zend Framework (19) ZendFramework2 (8)

вторник, 5 апреля 2011 г.

PHP. Подключение к MSSQL (локально).

Установлен MSSQL Server 2005 на Windows 7 Professional ...

Смотрим список процессов:
C:\Users\leon>tasklist
Имя образа                     PID Имя сессии          № сеанса       Память
========================= ======== ================ =========== ============
...
sqlservr.exe                  2408 Services                   0 1 505 316 КБ
...

C:\Users\leon>netstat -ano
 Имя    Локальный адрес        Внешний адрес          Состояние       PID
...
TCP    127.0.0.1:1434         0.0.0.0:0              LISTENING       2408
...

Таким образом убеждаемся, что сервер запущен и слушает порт 1434

Проверяем разрешен ли TCP/IP для SQL-сервера:

SQL Server Configuration Manager -> Network Configuration -> Protocols -> TCP/IP enabled

Документация драйвера MSSQL для PHP от Microsoft тут - http://msdn.microsoft.com/ru-ru/library/ee229547%28v=SQL.10%29.aspx

Качаю и ставлю драйвер MSSQL для PHP тут -
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=ccdf728b-1ea0-48a8-a84a-5052214caad9

Системные требования для установки драйвера - http://msdn.microsoft.com/ru-ru/library/cc296170%28v=SQL.90%29.aspx

Для PHP 5.3.x в php.ini добавляю следующее (dll положил в ext):

...
[PHP_SQLSRV]
extension=php_sqlsrv_53_nts_vc9.dll
...

Далее пытаюсь подключиться (подробности http://msdn.microsoft.com/ru-ru/library/cc296161%28v=sql.90%29.aspx):

/*
Connect to the local server using Windows Authentication and specify
the AdventureWorks database as the database in use. To connect using
SQL Server Authentication, set values for the "UID" and "PWD"
 attributes in the $connectionInfo parameter. For example:
$connectionInfo = array("UID" => $uid, "PWD" => $pwd)); 
*/
$serverName = "(local)";
$connectionInfo = array("Database" => "MyMSDb");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if( $conn )
{
     echo "Connection established.\n";
}
else
{
     echo "Connection could not be established.\n";
     die(print_r( sqlsrv_errors(), true));
}

//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------

/* Close the connection. */
sqlsrv_close($conn);

Получаю ошибку:

The SQL Server Driver for PHP requires the SQL Server 2008 Native Client ODBC Driver (SP1 or later) to communicate with SQL Server. That ODBC Driver is not currently installed. Access the following URL to download the SQL Server 2008 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712

Клиент стоял для MSSQL Server 2005, значит качаем и ставим для 2008
http://download.microsoft.com/download/0/E/6/0E67502A-22B4-4C47-92D3-0D223F117190/sqlncli.msi

Запускаю скрипт - ошибка:
...
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'leon-PC\leon'
...
Клиент работает - это хорошо, авторизация не проходит ...
Вместо MyMSDb ставим имя реальной базы данных и на локальной машине подключение проходит.

Connection established.

Комментариев нет:

Отправить комментарий