Home php教程 php手册 关于PHP无法连接MSSQL数据库的问题

关于PHP无法连接MSSQL数据库的问题

Jun 21, 2016 am 09:07 AM
connect mssql php server

数据|数据库|问题

今天配置了新服务器,配置是IIS+php,结果运行时发现php连接远程mssql数据库出错,出错代码如下:
Warning: mssql_connect(): Unable to connect to server:

想想以前都是没问题的,怎么回事呢?后来去网上搜索,发现一篇文章,才发现原来服务器是需要安装mssql才能用php连接mssql的,本来新服务器上我是不需要用到mssql的,但是现在没办法了,只能把它装上了,安装了mssql之后就没问题了。

我在想,如果是在linux上面的apache+php会怎样呢,不可能需要安装mssql吧,呵呵,晕了。

下面是找到的一篇文章。

php配置:
在php.ini文件里设置如下,找到 
;extension=php_mssql.dll  把前面的分号去掉 
找到extension_dir  =  d:\extension\ 
你的php.ini里面可能不是d:\extension\ 
改成在php安装目录下面的extensions目录下面的php_mssql.dll,所在的路径,如果你没有把它移动到其他地方(假设你的php安装路径是d:\php) 
就改成extension_dir=d:\php\extensions\ 
然后重新启动web服务器


这一点很容易做到,但是做完这样的设置后还是连不上,错误的信息如下:

MS SQL Server 数据库连接错误!请检查数据库主机变量设置是否正确!!!

而主机的变量设置我是一遍一遍的检查,那些设置是一点问题都没有的,翻遍网页,找到了下面的这点蛛丝马迹:


php.com资料:

I am trying to connect to SQL Server 2000 from PHP
I bumped to following warning:
Warning: mssql_connect(): Unable to connect to server: SERVER\Portal
....... on line 5

on line 5 there is:
$db_connect = mssql_connect('SERVER\Portal', 'sa', 'my_passwd');

I did the following
1.enabled php_mssql.dll extension in PHP.ini
2.every dll is in proper place(System32 or PHP folder),including ntwdblib.dll

I search lots of profile throught web ,but no one give me proper answer to resolve it.

after a few hour ,I found the problem was caused by
ntwdblib.dll ,which version is 7.00.839 ,when I replaced old ntwdblib.dll with the new

ntwdblib.dll ,which version is 8.00.194 ,all problem are solved.

We had some, read A LOT, of problems with MSSQL under Windows 2003.
We had 2 the same windows, php, php-ini, everything machines but only one could connect.

Unable to connect was the error message.

Finnaly we checked the version of ntwdblib.dll and the one distributed with PHP was 7.00....

and the version of the one on the SQL Server install was 8.00.... so we copied this one in

the php and apache dir and it worked.

问题就这样被找到了,惹祸的是它 ntwdblib.dll

ntwdblib.dll的主要作用是提供sql server连接服务。

我用的php版本是4.3.9,在安装它的服器的 windows/system32/ 下我查到ntwdblib.dll文件的版本是2000.2.8.0 ,这个版本支持的是sql server 7.0, 因为安装PHP时会把dlls下面的所有文件覆盖到系统

目录下,所以当我用它去连接 sql server 2000 的时候当然会是无法连接了。

后来我在一台正常安装sql server 2000 的服务器上查到 ntwdblib.dll的版本是 2000.80.2039.0,我把这个文件拷过去,覆盖掉以前的版本,重启服务器后,一切正常。
 
补充:如果数据库名的开头是数字时也会提示无法打开,这时要做的很简单,把数据库的名字用中括号 [ ]
括起来就搞定了,如 123bbs 改写成 [123bbs]就没有问题了,另外如果你的数据库名字与sql server中的保留字冲突的话也会出现这种情况,用中括号的方法一样可以解决。

最终,PHP无法正确连接sql server 2000的问题终于解决了,虽然耗费大半天的时间,但收获还是很大的,现在把它贴出来,也让遇到同样问题的兄弟们少走一些弯路。

 



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles