在线咨询
QQ咨询
服务热线
服务热线:13125520620
TOP

WIN和LINUX下PHP连接mssql的方法 - 数据库开发

发布时间:2011-11-12 浏览:4518

WIN下比较简单:

1.安装SQL服务器并添加PHP的MSSQL扩展.
2.使用以下代码连接并测试

<?php

$myServer = "localhost"; //主机
$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.0

2.编译PHP时.加上 --with-mssql=/freetbs的path

3.连接测试:

<?
$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";
}
?>

TAG
软件定制,软件开发,瀚森HANSEN,辽宁,沈阳,抚顺
0
该内容对我有帮助