你的数据库是本地的吗? 如果是就改为 $con=mssql_connect("localhost","sa","")or die("Couldn't connect to SQL Server on $myServer"); 不是就新建个用户吧 $con=mssql_connect("服务器IP地址/主机名","用户名","密码")or die("Couldn't connect to SQL Server on $myServer");
<?php class DB_Sql { var $Host = ""; var $Database = ""; var $User = ""; var $Password = ""; var $Link_ID = 0; var $Query_ID = 0; var $Record = array(); var $Row = 0;
var $Errno = 0; var $Error = ""; var $Auto_Free = 0; ## set this to 1 to automatically free results
function connect() { if ( 0 == $this->Link_ID ) { $this->Link_ID=mssql_pconnect($this->Host, $this->User, $this->Password); if (!$this->Link_ID) $this->halt("Link-ID == false, mssql_pconnect failed"); else mssql_select_db($this->Database, $this->Link_ID); } } function free_result(){ mssql_free_result($this->Query_ID); $this->Query_ID = 0; }
function query($Query_String) { if (!$this->Link_ID) $this->connect();
如果是就改为
$con=mssql_connect("localhost","sa","")or die("Couldn't connect to SQL Server on $myServer");
不是就新建个用户吧
$con=mssql_connect("服务器IP地址/主机名","用户名","密码")or die("Couldn't connect to SQL Server on $myServer");
class DB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = ""; var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row = 0;
var $Errno = 0;
var $Error = ""; var $Auto_Free = 0; ## set this to 1 to automatically free results
function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=mssql_pconnect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID)
$this->halt("Link-ID == false, mssql_pconnect failed");
else
mssql_select_db($this->Database, $this->Link_ID);
}
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String) {
if (!$this->Link_ID)
$this->connect();
# printf("<br>Debug: query = %s<br>\n", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0;
if (!$this->Query_ID) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
function next_record() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>]
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
}
$this->Row += 1;
$stat = 1;
} else {
if ($this->Auto_Free) {
$this->free_result();
}
$stat = 0;
}
return $stat;
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos);
$this->Row = $pos;
} function metadata($table) {
$count = 0;
$id = 0;
$res = array(); $this->connect();
$id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) {
$info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table;
$res[$i]["name"] = $info["name"];
$res[$i]["len"] = $info["max_length"];
$res[$i]["flags"] = $info["numeric"];
}
$this->free_result();
return $res;
}
function affected_rows() {
return mssql_affected_rows($this->Query_ID);
}
function num_rows() {
return mssql_num_rows($this->Query_ID);
}
function num_fields() {
return mssql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Field_Name) {
return $this->Record[strtolower($Field_Name)];
}
function p($Field_Name) {
print $this->f($Field_Name);
}
function halt($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MSSQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);
die("Session halted.");
}
}
?>
session_start();
$includepath=substr(__FILE__, 0, strlen(__FILE__) - strlen(basename(__FILE__)));
include($includepath."sqlserver.inc");
$sys_user="sa";
$sys_password="manager";
$sys_database="edu_db";$mydb=new DB_Sql();
$mydb->Host=$sys_host;
$mydb->User=$sys_user;
$mydb->Password=$sys_password;
$mydb->Database=$sys_database;
$mydb->connect();
/////////////////////////////////////////////////////////////////////////////
$conn=new DB_Sql();
$conn->User=$sys_user;
$conn->Host=$sys_host;
$conn->Password=$sys_password;
$conn->Database=$sys_database;
$conn->connect();
///////////////////////////////////////////
$conn1=new DB_Sql();
$conn1->User=$sys_user;
$conn1->Host=$sys_host;
$conn1->Password=$sys_password;
$conn1->Database=$sys_database;
$conn1->connect();
/////////////////////////////////////////
$conn2=new DB_Sql();
$conn2->User=$sys_user;
$conn2->Host=$sys_host;
$conn2->Password=$sys_password;
$conn2->Database=$sys_database;
$conn2->connect();?>
这个文件声明了3个数据库连接
改成$sys_database="Northwind";
下面的程序就可以用了。
1。把第一贴保存为sqlserver.inc
2。把地二贴保存为config.php
下面是例成:
<?php
$query="select * from Orders";
$mydb->query($query);
while ($mydb->next_record())
{
echo $mydb->f(order_id)." ".$mydb->f(ShipName);
echo "<br>";
}
?>
其他的例成我也有。
不过这个是最基本的
第二条错误说明你还没有连接到服务器。
第一条错误说明你的SQL用户名或密码登录不正确。
我建议:
1、检查你的PHP是否能正常工作;
2、检查你的SQL用户名能否正常登录,可以直接在SQL Server中检测;建议不要使用默认的Windows用户名,要用SQL用户名。
最后,如果实在不行,就重装PHP,然后重装SQL Server 2000,选择建立SQL用户名登录。
我就是这样做的,很奇怪,一下了就搞定了。