帖个类给你
<?php
/*
* Session Management for PHP3
*
* (C) Copyright 1998 Cameron Taggart ([email protected])
* Modified by Guarneri carmelo ([email protected])
* Modified by Cameron Just ([email protected])
*
* $Id: db_mssql.inc,v 1.5 2002/03/19 22:26:12 layne_weathers Exp $
*/
# echo "<BR>This is using the MSSQL class<BR>";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
var $PConnect = 0; ## Set to 1 to persistent database connections
/* public: constructor */
function DB_Sql($query = "") {
$this->query($query);
} function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* Handle defaults */
if ("" != $Database) $this->Database = $Database; if ("" != $Host) $this->Host = $Host; if ("" != $User) $this->User = $User; if ("" != $Password) $this->Password = $Password; if ( 0 == $this->Link_ID ) {
if(!$this->PConnect) {
$this->Link_ID = mssql_connect($this->Host, $this->User, $this->Password);
} else {
$this->Link_ID = mssql_pconnect($this->Host, $this->User, $this->Password);
}
if (!$this->Link_ID)
$this->halt("connect($Host, $User, \$Password) failed.");
else
mssql_select_db($Database, $this->Link_ID);
}
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
$Query_String = $this->mssql_select($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() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
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.");
}
function mssql_select($sql)
{
if(eregi(" limit ", $sql))
{
//去连续空格
while(eregi(" ", $sql))
{
$sql = str_replace(" "," ",$sql);
} $sql_array = explode(" ",$sql); //取得部分重要的数组索引
$i = 0 ;
while($sql_array[$i])
{
if($sql_array[$i]=="from")
{
$from_id = $i;
}
if($sql_array[$i]=="limit")
{
$limit_id = $i;
}
if($sql_array[$i]=="order")
{
$order_id = $i;
}
$i++;
}
$last_id = $i-1;
$two_num = explode(",",$sql_array[$limit_id+1]);
$totle_num = $two_num[0]+$two_num[1]; //当sql语句用倒序排列的时候
if(eregi(" desc ", $sql))
{
$sql_return = "select ";
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ( ";
$sql_return .= "select top ";
$sql_return .= $two_num[1]." ";
//echo $from_id;
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .=" ( ";
$sql_return .= "select top ";
$sql_return .= $totle_num." ";
for($i=1;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ) as totleResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
if($sql_array[$i] == "desc")
continue;
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ) as allResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
} //顺序排列
if(!eregi(" desc ", $sql))
{
$sql_return = "select ";
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ( ";
$sql_return .= "select top ";
$sql_return .= $two_num[1]." ";
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .=" ( ";
$sql_return .= "select top ";
$sql_return .= $totle_num." ";
for($i=1;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ) as totleResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
if($i == $order_id+2)
{
$sql_return .= "desc ";
}
}
$sql_return .= " ) as allResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
}
return $sql_return;
}
else
return $sql;
}
}
?>
<?php
/*
* Session Management for PHP3
*
* (C) Copyright 1998 Cameron Taggart ([email protected])
* Modified by Guarneri carmelo ([email protected])
* Modified by Cameron Just ([email protected])
*
* $Id: db_mssql.inc,v 1.5 2002/03/19 22:26:12 layne_weathers Exp $
*/
# echo "<BR>This is using the MSSQL class<BR>";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
var $PConnect = 0; ## Set to 1 to persistent database connections
/* public: constructor */
function DB_Sql($query = "") {
$this->query($query);
} function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* Handle defaults */
if ("" != $Database) $this->Database = $Database; if ("" != $Host) $this->Host = $Host; if ("" != $User) $this->User = $User; if ("" != $Password) $this->Password = $Password; if ( 0 == $this->Link_ID ) {
if(!$this->PConnect) {
$this->Link_ID = mssql_connect($this->Host, $this->User, $this->Password);
} else {
$this->Link_ID = mssql_pconnect($this->Host, $this->User, $this->Password);
}
if (!$this->Link_ID)
$this->halt("connect($Host, $User, \$Password) failed.");
else
mssql_select_db($Database, $this->Link_ID);
}
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
$Query_String = $this->mssql_select($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() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
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.");
}
function mssql_select($sql)
{
if(eregi(" limit ", $sql))
{
//去连续空格
while(eregi(" ", $sql))
{
$sql = str_replace(" "," ",$sql);
} $sql_array = explode(" ",$sql); //取得部分重要的数组索引
$i = 0 ;
while($sql_array[$i])
{
if($sql_array[$i]=="from")
{
$from_id = $i;
}
if($sql_array[$i]=="limit")
{
$limit_id = $i;
}
if($sql_array[$i]=="order")
{
$order_id = $i;
}
$i++;
}
$last_id = $i-1;
$two_num = explode(",",$sql_array[$limit_id+1]);
$totle_num = $two_num[0]+$two_num[1]; //当sql语句用倒序排列的时候
if(eregi(" desc ", $sql))
{
$sql_return = "select ";
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ( ";
$sql_return .= "select top ";
$sql_return .= $two_num[1]." ";
//echo $from_id;
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .=" ( ";
$sql_return .= "select top ";
$sql_return .= $totle_num." ";
for($i=1;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ) as totleResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
if($sql_array[$i] == "desc")
continue;
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ) as allResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
} //顺序排列
if(!eregi(" desc ", $sql))
{
$sql_return = "select ";
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ( ";
$sql_return .= "select top ";
$sql_return .= $two_num[1]." ";
for($i=1;$i<=$from_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .=" ( ";
$sql_return .= "select top ";
$sql_return .= $totle_num." ";
for($i=1;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
$sql_return .= " ) as totleResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
if($i == $order_id+2)
{
$sql_return .= "desc ";
}
}
$sql_return .= " ) as allResult ";
for($i=$from_id+2;$i<$limit_id;$i++)
{
$sql_return .= $sql_array[$i];
$sql_return .= " ";
}
}
return $sql_return;
}
else
return $sql;
}
}
?>
我的操作系统是:win2003 sql2000 php4.3.8
我把警告显示打开后 结果如下
Warning: mssql_connect(): message: 用户 'SITAR-O7G2CSL5J\IUSR_SITAR-O7G2CSL5J' 登录失败。 (severity 14) in D:\ttch\dm001\test\index.php on line 4Warning: mssql_connect(): Unable to connect to server: localhost in D:\ttch\dm001\test\index.php on line 4