php adodb MSSQL 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10881599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:16:43  来源:igfitidea点击:

php adodb MSSQL connection

phpsql-serveradodb-php

提问by Shane

I have a linux server that I'm trying to use php adodb to connect to a MSSQL server.

我有一台 linux 服务器,我正在尝试使用 php adodb 连接到 MSSQL 服务器。

include('adodb5/adodb.inc.php'); 

$conn =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=MSSERVER;Database=Northwind;";
$conn->Connect($dsn,'sa','password')or die("Unable to connect to server");

I've install mssql through yum etc and I know the server can connect to it as I've tried the following:

我已经通过 yum 等安装了 mssql 并且我知道服务器可以连接到它,因为我已经尝试了以下操作:

$db = @mssql_connect("MSSERVER","sa","password") or die("Unable to connect to server");
mssql_select_db("Northwind");

// Do a simple query, select the version of 
// MSSQL and print it.
$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);

echo $row[0];

// Clean up
mssql_free_result($version);

Any ideas why my adodb wont connect, or any examples on how I can connect would be much appreciated.

任何我的 adodb 无法连接的想法,或有关如何连接的任何示例,将不胜感激。

回答by Shane

I've solved this by looking at this forum: http://ourdatasolution.com/support/discussions.html?topic=4200.0

我通过查看这个论坛解决了这个问题:http: //ourdatasolution.com/support/discussions.html?topic=4200.0

The correct code is:

正确的代码是:

<?php
include("adodb5/adodb.inc.php"); 
//create an instance of the  ADO connection object
$conn =&ADONewConnection ('mssql');
//define connection string, specify database driver
$conn->Connect('xxx.xxx.x.xxx:1400', 'user', 'password', 'DbName');
//declare the SQL statement that will query the database
$query = "select * from table";
$rs = $conn->execute($query);
//execute the SQL statement and return records
$arr = $rs->GetArray();
print_r($arr);
?> 

Hope that helps somebody else.

希望能帮助别人。

回答by Radon8472

With php 5.3 of above the php_mssqlmodul is no longer supported for windows.

对于 php 5.3 以上的php_mssql模块,windows 不再支持。

A Solution is to download the MicroSoft PHP Driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098.

解决方案是从http://www.microsoft.com/en-us/download/details.aspx?id=20098下载 MicroSoft PHP 驱动程序。

This installer will extract the modul dll files to you php extension directory.

此安装程序会将模块 dll 文件解压缩到您的 php 扩展目录。

Include the correct version in you php ini (e.g. like this for php 5.3 ThreadSafe):

在你的 php ini 中包含正确的版本(例如,对于 php 5.3 ThreadSafe 就是这样):

extension=php_sqlsrv_53_ts.dll  

After this you can use adboDb again, but you have to use mssqlnativeas adodbtype. And the connection with ip and port didnt work for me, but ipaddress\\SERVERNAMEworked (see examplecode)

之后你可以再次使用adboDb,但你必须使用mssqlnativeadodbtype。与 ip 和端口的连接对我不起作用,但ipaddress\\SERVERNAME有效(参见示例代码)

<?php include("adodb5/adodb.inc.php");  
//create an instance of the  ADO connection object
$conn =&ADONewConnection ('mssqlnative'); 
//define connection string, specify database driver 

// $conn->Connect('xxx.xxx.x.xxx:1400', 'user', 'password', 'DbName'); 
$conn->Connect('xxx.xxx.x.xxx\SERVERNAME', 'user', 'password', 'DbName'); 

//declare the SQL statement that will query the database 
$query = "select * from table"; 
$rs = $conn->execute($query); 
//execute the SQL statement and return records 
$arr = $rs->GetArray(); 
print_r($arr); ?>