apache与PHP成功连接
phpinfo可以显示
但是与数据库连接不上
test.php代码<?php
$conn=mssql_connect("ZHAIYANG-PC\SQL2008","sa","123456");
if($conn)
echo"连接成功!";
else
echo"连接失败!";
?>提示以下
Fatal error: Call to undefined function mssql_connect() in D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php on line 2
我的数据库名称为“ZHAIYANG-PC”PHP目录下有php_mssql.dll
php.ini里extension=php_mssql.dill前面没有分号
ntwdblib.dll也放在了system32下
鼓捣了半天最终还是上面的提示,求助~
phpinfo可以显示
但是与数据库连接不上
test.php代码<?php
$conn=mssql_connect("ZHAIYANG-PC\SQL2008","sa","123456");
if($conn)
echo"连接成功!";
else
echo"连接失败!";
?>提示以下
Fatal error: Call to undefined function mssql_connect() in D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php on line 2
我的数据库名称为“ZHAIYANG-PC”PHP目录下有php_mssql.dll
php.ini里extension=php_mssql.dill前面没有分号
ntwdblib.dll也放在了system32下
鼓捣了半天最终还是上面的提示,求助~
mssql是对微软的ms sql server的;
mysql才是对php+mysql配置的.
phpinfo();
?>看看有没有sqlsrv这项。
如果没有,MS好像针对sql2005以上版本有个sql server driver for php,去下载解压后找到对应php版本的驱动,有很多个,应该选在里面带有ts-vc9.0的那个复制到php扩展目录ext中,然后再php.ini中extension=后面添加你刚才复制的驱动文件名,再看看.
sql server driver for php有个版本1.1好像不能用我在想提示第二行错误
不是显示出来“连接失败!”
是不是服务器名字出错?
$server="localhost";
$user="root";(改成你的用户名)
$password="root";(改成你的密码)
$database="pkusz";(这是数据库名)
$conn=mysql_connect($server,$user,$password);
mysql_select_db($database,$conn);(连接后选择数据库名)
$myUser = "sa"; //用户名
$myPass = "password"; //密码
$myDB = "Northwind"; //MSSQL库名$s = @mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer"); $d = @mssql_select_db($myDB, $s)
or die("Couldn't open database $myDB"); $query = "SELECT TitleOfCourtesy+' '+FirstName+' '+LastName AS Employee ";
$query .= "FROM Employees ";
$query .= "WHERE Country='USA' AND Left(HomePhone, 5) = '(206)'"; $result = mssql_query($query);
$numRows = mssql_num_rows($result); echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["Employee"] . "</li>";
} ?> 代码来源:PHP手册 -------------------------------------------------LINUX下的方法如下:(以TCP/IP 远程连接MSSQL)1.安装freetds后.配置好.一般是这样:[MSSQL_2000]
host = 192.168.0.25
port = 1433
tds version = 8.02.编译PHP时.加上 --with-mssql=/freetbs的path3.连接测试:<?
$msconnect=mssql_connect("MSSQL_2000","sa","");
$msdb=mssql_select_db("Northwind",$msconnect);
$msquery = "select titleofcourtesy,firstname,lastname from employees";
$msresults= mssql_query($msquery);
while ($row = mssql_fetch_array($msresults)) {
echo "<li>" . $row['titleofcourtesy'] . " " . $row['firstname'] . " " . $row['lastname'] . "</li>\n";
}
?>
来源: 字体:[大 中 小]
今天配置了新服务器,配置是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.dllntwdblib.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的问题终于解决了,虽然耗费大半天的时间,但收获还是很大的,现在把它贴出来,也让遇到同样问题的兄弟们少走一些弯路
文章作者:Enjoy 转载请注明原文链接。
本机用php连接远程MSSQL,代码
<?php
$linkId = mssql_connect('IP地址','用户名','密码');
?>提示
引用
mssql_connect() [function.mssql-connect]: Unable to connect to server
而同事就可以连,他比较了ntwdblib.dll这个文件,版本都是2000.2.8.0。唯一不同的是他的机子装了MSSQL,怀疑是这个原因。搜索了下,发现确实是这个原因。也有不少人碰到装了MSSQL就可以连远程MSSQL,没装则不行。试验了下,发现原因还是在于NTWDBLIB.DLL这个文件。我本机是php5自带的,版本为2000.2.8.0,他也是。在MSSQL光盘中搜索这个文件,找到NTWDBLIB.DLL,版本是2000.80.194.0。将这个文件,Copy到php安装目录和c:\windows\system32目录下,停掉IIS或Apache。重启,连接成功:)为方便碰到此问题的朋友,我打包了NTWDBLIB.DLL这个文件,下载后解压即可:
下载文件
点击这里下载文件
后来我用的方法是
1) 加载 php_sqlsrv_53_ts.dll 到apache 2.2(我用PHP 5.3.8 thread safe 版本)
你用 PHP 5.2 的话你得下载 php_sqlsrv_52_ts.dll 或者 php_sqlsrv_52_nts.dll 取决你PHP 版本
2) Coding (我以SQL Server Authentication方式)
<?php
$server_name = '服务器名';
$username='登入名';
$password='密码';
$database_name='数据库名';
$connect_info = array('UID'=>$username,'PWD'=>$password,'Database'=>$database_name);
$conn = sqlsrv_connect($server_name, $connect_info);
if($conn) echo '连接成功';
else echo '连接失败';
?>
希望能帮到你
不用写你的计算机名
$conn=mssql_connect('127.0.0.1','sa','123456');
日,没有看完提问,要么你php.ini 文件没有加载sqlserver 驱动库的地址或者就是sqlserver 驱动文件没在目录下