PHP SQL Server Couldn't connect

倖福魔咒の 提交于 2019-12-07 20:59:01

问题


I want to ask you about how to connect to MSSQL Server 2005 using PHP?

I got error like this :

Connection could not be established. Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )

and here is my PHP code :

<?php
    $serverName = "192.168.183.249\MSSQLSERVER, 1542"; //serverName\instanceName, portNumber default is 1433)
    $connectionInfo = array( "Database"=>"SEI_AproCS", "UID"=>"sa", "PWD"=>"");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    if( $conn ) {
     echo "Connection established.<br />";
    }else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
    }
?>

Please help to advice.

THanks


回答1:


You just need to install driver from this link PHP Driver

You can follow this link for tutorial PHP MSSQL Tutorial

An other option is to connect to mssql using DSN ,
and this easy way you just to enable mssql extension in php

And follow this link to create DSN Create DSN in windows

Also follow this link to connect using php and DSN Connect php using DSN




回答2:


To connect with sql server you need to add dll file for sql server in the directory

wamp\bin\php\php5.3.0\ext



回答3:


Try PDO:

//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password);  // works with proper driver for ODBC and PHP ODBC.

I could not get the first line to work due to weird compiler version incompatibilities, but the second one worked fine after installing Microsoft ODBC Driver 11 for SQL Server

PHP Version 5.3.0 has built-in ODBC support, according to php.ini, but that still lists an active extension=php_pdo_odbc.dll here.




回答4:


You instal driver. ODBC

  • msodbcsql_x64.msi #System Operation 63
  • msodbcsql_x86.msi #System Operation 86/32

link download https://www.microsoft.com/en-us/download/details.aspx?id=36434

good luck



来源:https://stackoverflow.com/questions/19924024/php-sql-server-couldnt-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!