php连接微软MSSQL(sql server)完全攻略

2019-05-02 12:00:07王冬梅
0.82 + 20110906 patches Download! FTP Download!

2、 FreeTDS 需要安装 .NET Framework v1.1 ,你可以到微软的网站去下载。或者你去Frank's site 下载需要的DLL文件,并保存到你的/PHP根目录下面。

3、在php配置文件 /PHP/php.ini 中添加:

extension=php_dblib.dll

4、当php引擎启动FreeTDS模块的时候需要传递一些信息,使得FreeTDS能够连接到它的默认的数据库。因此它的需要在freetds.conf中定义数据库连接的基本信息,该文件在其根目录下,可以按照你的情况来进行修改:

[global]
host = xxx.xxx.xxx.xxx (host name or ip of the MSSQL server)
port = 1433
client charset = UTF-8
tds version = 8.0
text size = 20971520
5、创建config.php文档来定义数据库连接参数:

$CFG->dbtype = 'mssql'; // Required
$CFG->dbhost = 'localhost'; // assuming MS SQL is on the same server, otherwise use an IP
$CFG->dbname = 'moodle';  // or whatever you called the database you created
$CFG->dbuser = 'yourusername'; // I usually use the 'sa' account (dbowner perms are enough)
$CFG->dbpass = 'yourpassword';
$CFG->dbpersist = false;
$CFG->prefix = 'mdl_';  //Prefix, you can change it, but NEVER leave it blank.

6、 重启你的网站,如果还是没有连接到你的数据库的话,在 /PHP/php.ini文件中将display_startup_errors改为"On",当你解决了这些问题之后再将错误报告改为“Off”;
7、测试你的网站,建立test.php文件,代码如下,访问http://localhost/test.php进行测试

<?php
	$link = mssql_connect('localhost', 'db_user', 'db_password');
	if(!$link) {
		echo'Could not connect';
		die('Could not connect: ' . mssql_error());
	}
	echo'Successful connection';
	mssql_close($link);
?>

好了windows下使用FreeTDS网上的资料一大堆就不再讲了,至此,此篇日志结束。

相关文章 大家在看