WIN和LINUX下PHP连接mssql的方法

WIN下比较简单:

1.安装SQL服务器并添加PHP的MSSQL扩展.(参见文章:win2000搭建PHP+Sql Server2000环境)

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

你可能感兴趣的:(数据库,windows,PHP,linux,MSSQL)