php的mssql数据库连接类实例
本文实例讲述了php的mssql数据库连接类实例代码,分享给大家供大家参考。
具体实现代码如下:
classDB_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; ##setthisto1toautomaticallyfreeresults
functionDB_Sql($query=""){
$this->query($query);
}
functionconnect(){
if(0==$this->Link_ID){
$this->Link_ID=mssql_connect($this->Host,$this->User,$this->Password);
if(!$this->Link_ID)
$this->halt("Link-ID==false,mssql_pconnectfailed");
else
@mssql_select_db($this->Database,$this->Link_ID);
}
}
functionfree_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID=0;
}
functionquery($Query_String)
{
/*Noemptyqueries,please,sincePHP4chokesonthem.*/
if($Query_String=="")
/*Theemptyquerystringispassedonfromtheconstructor,
*whencallingtheclasswithoutaquery,e.g.insituations
*likethese:'$db=newDB_Sql_Subclass;'
*/
return0;
if(!$this->Link_ID)
$this->connect();
# printf("<br>Debug:query=%s<br>",$Query_String);
$this->Query_ID=mssql_query($Query_String,$this->Link_ID);
$this->Row=0;
if(!$this->Query_ID){
$this->Errno=1;
$this->Error="GeneralError(TheMSSQLinterfacecannotreturndetailederrormessages).";
$this->halt("InvalidSQL:".$Query_String);
}
return$this->Query_ID;
}
functionnext_record(){
if($this->Record=mssql_fetch_row($this->Query_ID)){
//addtoRecord[<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;
}
functionseek($pos){
mssql_data_seek($this->Query_ID,$pos);
$this->Row=$pos;
}
functionmetadata($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="GeneralError(TheMSSQLinterfacecannotreturndetailederrormessages).";
$this->halt("Metadataqueryfailed.");
}
$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;
}
functionaffected_rows(){
//NotasupportedfunctioninPHP3/4. ChrisJohnson,16May2001.
// returnmssql_affected_rows($this->Query_ID);
$rsRows=mssql_query("Select@@rowcountasrows",$this->Link_ID);
if($rsRows){
returnmssql_result($rsRows,0,"rows");
}
}
functionnum_rows(){
returnmssql_num_rows($this->Query_ID);
}
functionnum_fields(){
returnmssql_num_fields($this->Query_ID);
}
functionnf(){
return$this->num_rows();
}
functionnp(){
print$this->num_rows();
}
functionf($Field_Name){
return$this->Record[strtolower($Field_Name)];
}
functionp($Field_Name){
print$this->f($Field_Name);
}
functionhalt($msg){
printf("</td></tr></table><b>Databaseerror:</b>%s<br>",$msg);
printf("<b>MSSQLError</b>:%s(%s)<br>",
$this->Errno,
$this->Error);
die("Sessionhalted.");
}
}
希望本文所述对大家的PHP程序设计有所帮助。