문제

I'm working on a web application that has to connect through PHP & PDO to MS SQL 2008 R2. I have successfully installed the drivers and also checked it with:

var_dump(PDO::getAvailableDrivers());

I use two Windows servers (both are 2008) one for SQL database and the other for XAMPP. We have around eight programs that are connected to the SQL server from other servers, so my guess is that the connection problem is not the SQL server itself.

Here you have a very simple example to insert into a MS SQL database table from the application:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){      
$company        = $_POST['company'];    
$name           = $_POST['name'];   
$firstname      = $_POST['firstname'];  
$number         = $_POST['number'];     
$host           = $_POST['host'];   
$licenseplate           = $_POST['plate'];  
$reason         = $_POST['site'];   
$timein         = date('Y-m-d H:i:s');

$db = new PDO("sqlsrv:Server=MSSQLSERVER2,1433;Database=visitormod", "**************", "******************");   $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);       
$sql = "INSERT INTO visitors (visitor_name, visitor_firstname, visitor_company, visitor_number, visitor_plate, visitor_in) VALUES (:lname, :fname, :company, :pnumber, :lplate, :tin)";     
$q = $db->prepare($sql);    
$q->execute(array(':lname'=>$name, ':fname'=>$firstname, ':company'=>$company, ':pnumber'=>$number, ':lplate'=>$licenseplate, ':tin'=>$timein));

Here is some extra info:

Name of the host of the SQL server (computername itself) = SQLSERVER

Name of the SQL SERVER (under SQL Server Management Studio = Where I connect to) = MSSQLSERVER2

Here are my questions:

I am not able to connect successfully to the database my guess is its related this

$db = new PDO("sqlsrv:Server=MSSQLSERVER2,1433;Database=visitormod", "**************", "******************");

Here I use Server=MSSQLSERVER2 but that is just the name of the server in SQL server management studio. Nowhere I point to the server itself maybe that is my problem and I need an ODBC connection? I read on the internet that is just necessary on a Linux environment?

Can someone help and explain me what I am doing wrong? Do I have to make an extra connection?

The last question I would like to ask is where do I see which port number I have to connect to?

i.e.

new PDO("sqlsrv:Server=MSSQLSERVER2,1433;  

This is the error I receive on my page:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: go.microsoft.com/fwlink/?LinkId=163712';

도움이 되었습니까?

해결책

The clue to solving your problem is in the error message:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IMSSP]: This extension requires the Microsoft SQL Server 2012 Native Client ODBC Driver to communicate with SQL Server. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: go.microsoft.com/fwlink/?LinkId=163712';

It would appear that you don't have the SQL Server Native Client drivers installed.

Browse to this URL:

Microsoft® SQL Server® 2012 Feature Pack

Scroll down until you see:

Microsoft® SQL Server® 2012 Native Client

Download and run the installer for the X86 Package (http://go.microsoft.com/fwlink/?LinkID=239647&clcid=0x409)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top