PHP连接sql server 2005环境配置及问题解决
这篇文章主要介绍了PHP连接sql server 2005环境配置及问题解决,需要的朋友可以参考下
一、Windows下PHP连接SQLServer 2005
设定:安装的Windows操作系统(Win7 或XP均可,其他系统暂未测试),在C盘下;PHP的相关文件位于c:/PHP下面,其配置文件php.ini位于C:\Windows下面。
连接前配置系统:
1、检查文件 php\ntwdblib.dll, 默认PHP的安装目录下面有一个,不能连接再替换。
下载正确版本的 ntwdblib.dll (2000.80.194.0)
2.配置php
a、打开php.in将extension=php_mssql.dll的注释符号去掉。
b、打开php.in将mssql.secure_connection = Off改为on。
c、将php_mssql.dll拷贝到php.in中extension_dir 指定的目录或者系统system32目录下, 位于/PHP/ext。
以上步骤完成后需要重启apache。
注意:实际使用中发现 如果通过php压缩文件手工安装php到iis下,必须重启机器而不仅仅是iis。
3.配置sqlserver
a. 运行 SQL Server 配置管理器:SQL Server Configuration Manager,打开协议 Protocols
b. 允许命名管道 "named pipes" 和 "tcp/ip"
c. 右键点击 "tcp/ip",打开属性 Properties 标签 "IP 地址"
d. 在 TCP 动态端口 "TCP Dynamic Ports" 填入 1433,注意分配正确的IP地址。
e. 重启 SQL Server
4.使用以下方式连接MS SQL Server 2005:
代码如下,另存为test.php:
//链接数据库
$conn=mssql_connect('localhost','sa','123456');
mssql_select_db('gu_dde',$conn);
//query语句
$Query="select * from dde_top";
$AdminResult=mssql_query($Query);
//输出结果
$Num=mssql_num_rows($AdminResult);
for($i=0;$i
输入
进行访问:
5.FAQ常见问题:
1报错:
Fatal error: Call to undefined function mssql_connect()
解决:
使用MSSQL_系列函数
要使用这两种都需要在php.ini进行设定:
(1)允许 DCOM,需要将php.ini中的 ;com.allow_dcom=TRUE前的分号";"去掉。
(2)使用MSSQL扩展,需要php.ini中的 ;extension=php_mssql.dll前的分号";"去掉。(关键)
(3)确认extension_dir为正确路径,以本机为例:extension_dir = "c:\php\ext"。
(4)如果仍然机器报错说找不到c:\php\ext\php_mssql.dll但明明存在这个文件。
解决方法:将php_mssql.dll,ntwdblib.dll拷贝到系统目录\system32下重启测试。。
(注:上面两个dll文件不在相同目录下,我的为c:\php\ext\php_mssql.dll;c:\php\ntwdblib.dll)
另外设置好了后记得重启服务器哦。
已经启用是否选择是
确认服务器正确之后,再确认ntwdblib.dll 文件位置是否放到了 c:\windows\system32下
同时要保证ntwdblib.dll 这个文件的版本和sqlserver的版本对应:
下面是对应关系:
1.ntwdblib.dll 版本为 2000.2.8.0 是 对应 SqlServer2000(这个是网络查资料和猜测,没装2000)
2.ntwdblib.dll 版本为 2000.80.194.0 是 对应 SqlServer2005(这个是用实验证明可以用,本人就是用笔记本装了2005)
3.ntwdblib.dll 版本为 2000.80.2039 是 对应 SqlServer2008(这个是猜测没有装2008)
6.其他问题:
如果php apache Sql Server2005都在同一台机器上,访问基本没有问题了。
如果Sql Server2005和php机器是分离的,需要确认ping sqlserver所在机器的机器名能通,如过不通,修改php所在机器的\system32\drivers\etc下的hosts文件,增加一行 sqlserver所在机器的机器ip sqlserver所在机器的机器名字。
如果还是无法访问,需要确认php所在的机器有无暗转mdac。要不索性安装一下sqlserver的客户端好了。
解决问题如下:
1.下载两个文件 php_mssql.dll 和 ntwdblib.dll
php_mssql.dll 如果这个没有复制到c:\windows\system32下,就很容易出现
ntwdblib2093.dll 这个文件要注意版本,不然后面搞得很郁闷.
二、Linux下php连接sql server 2005
1、首先根据需要安装LAMP环境中的除php外其他组件。 在Linux中使用freetds连接MS SQL 安装freetds
./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld --enable-shared --enable-static
make
make install
重要提示一:这一步需要注意的是--with-tdsver
2、配置freetds
server2005对应的tds version是7.2(貌似网上能查到的都写8.0)
配置/usr/local/freetds/etc/freetds.conf
# A typical Microsoft server
[1.1.1.21]
host = 1.1.1.21
port = 1433
tds version = 7.0
重要提示:在调用mssql_connect函数时,,需要注意的是,其第一个参数应该是[1.1.1.21](即在freetds.conf里的配置),而不是sql server的IP!
3、编译安装php
编译php的配置项根据需要设置,但是必须要加入的配置项为:--with-mssql=/usr/local/freetds/
注意:如果编译php加载freetds 时候出现不能找到freetds目录,则需要以下操作(官方的Faq):
touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a
4、安装完php以后,所有准备工作都完成了,下面就可以重启apache,就可以用php调用sql server 2005了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
