Some relevant experience and considerations of php connecting mssql

  • 2020-05-30 19:42:19
  • OfStack

In order for PHP to connect to MSSQL, the system needs to install MSSQL,PHP, and in the configuration in PHP.ini, will
; extension= php_mssql.dll; To get rid of
1. Connect MSSQL
 
$conn=mssql_connect(" Instance name or server IP"," The user name "," password "); 
// Test the connection  
if($conn) 
{ 
echo" The connection is successful "; 
} 

2. Select the database to connect to
 
mssql_select_db("dbname"); 

3. Execute the query
 
$rs=mssql_query("selecttop1id,usernamefromtbname",$conn); 
 Or just execute it update,insert Such statements , You don't have to assign a value to the return result  
mssql_query("updatetbnamesetusername='niunv'whereid=1"); 

4. Get the number of rows in the recordset
 
echomssql_num_rows($rs); 

5. Get the recordset
 
if($row=mssql_fetch_array($rs)) 
{ 
$id=$row[0];// To obtain ID The field values  
$username=$row[1];// To obtain username The field values  
} 

6. Get the ID of the new record
The id field is set to IDENTITY field. After the insert statement is executed, a @@IDENTITY global variable value will be generated, and the ID of the last newly added record will be found out.
 
mssql_query("insertintotbname(username)values('nv')",$conn); 
$rs=mssql_query("select@@IDENTITYasid",$conn); 
if($row=mssql_fetch_array($rs)) 
{ 
echo$row[0]; 
} 

7. Release the recordset
 
mssql_free_result($rs); 

8. Close the connection
 
mssql_close($conn); 

Note: it is easier to operate MSSQL with PHP than to connect MYSQL with ASP, so when MSSQL and MYSQL coexist, it is easier and easier to use PHP with MSSQL to operate MYSQL and MSSQL coexist.
1. At least install mssql client on web server
2. Open php.ini; Remove the semicolon before extension= php_mssql.dll
If necessary: extension_dir needs to be specified
3. php is recommended < =4.0.9 < =5.0.3 I haven't connected 4.010 and 5.0.3 yet
4. The connection page of the database can get the corresponding class on phpe.net
Below is one class that I modified according to there
 
<?php 
/** 
*mssql Database connection class  
**/ 
classSQL{ 
var$server; 
var$userName; 
var$passWord; 
var$dataBase; 
var$linkID=0; 
var$queryResult; 
var$lastInsertID; 
var$pageNum=0;// Paging with --- There are several pieces of data  
var$ER; 
/** 
* The constructor  
**/ 
functionSQL($Server='',$UserName='',$PassWord='',$DataBase=''){ 
$this->server=$Server; 
$this->userName=$UserName; 
$this->passWord=$PassWord; 
$this->dataBase=$DataBase; 
} 
/** 
* Database connection  
**/ 
functiondb_connect(){ 
$this->linkID=mssql_pconnect($this->server,$this->userName,$this->passWord); 
if(!$this->linkID){ 
$this->ER="db_connect($this->server,$this->userName,$this->passWord)error"; 
return0; 
} 
if(!mssql_select_db($this->dataBase,$this->linkID)){ 
$this->ER="mssql_select_db($this->dataBase,$this->lastInsertID)error"; 
return0; 
} 
return$this->linkID; 
} 
/**public 
*function:Checkthedatabase,ifexistthenselect 
*exist:return1 
*notexist:return0 
*/ 
functionselectDatabase(){ 
if(mssql_select_db($this->dataBase)) 
return1; 
else 
return0; 
} 
/** 
* Data manipulation  
**/ 
functionquery($Str){ 
if($this->linkID==0){ 
$this->ER=" The database is not connected yet!! "; 
} 
$this->queryResult=mssql_query($Str); 
//$this->queryResult=mssql_query($Str,$this->linkID); 
if(!$this->queryResult){ 
$this->ER="$Str. Failed operation ,queryerror !!!!! "; 
return0;//**************** for php4.3.9 Error using the above version 1 
} 
return$this->queryResult; 
} 
/** 
* Data acquisition  
**/ 
functionfetch_array($result){ 
if($result!="")$this->queryResult=$result; 
$rec=mssql_fetch_array($this->queryResult); 
if(is_array($rec)){ 
return$rec; 
} 
//$this->ER=" No data obtained! "; 
return0; 
} 
/**public 
*function:FreetheQueryResult 
*successreturn1 
*failed:return0 
*/ 
functionfreeResult($result=""){ 
if($result!="")$this->queryResult=$result; 
returnmssql_free_result($this->queryResult); 
} 
/** 
* Gets the number of rows affected  
* Gets the number of rows that have been manipulated  
**/ 
functionnum_rows($result=""){ 
if($result!=""){ 
$this->queryResult=$result; 
$row=mssql_num_rows($this->queryResult); 
return$row; 
} 
} 
/** 
* Get query results --- multiple  
**/ 
functionresult_ar($str=''){ 
if(empty($str)){ 
return0; 
} 
$back=array(); 
$this->queryResult=$this->query($str); 
while($row=$this->fetch_array($this->queryResult)){ 
$back[]=$row; 
} 
return$back; 
} 
/** 
* Database information paging  
*$Result Database operation  
*str==sql statements  
*page== What page  
*showNum== According to a few pages  
*/ 
functionpage($Str,$Page=0,$ShowNum=5){ 
$back=array();// Return the data  
$maxNum=0; 
if($Str==""){ 
$this->ER=" There is no data "; 
return0; 
} 
$this->queryResult=$this->query($Str); 
if($this->queryResult){ 
if($Page==""){ 
$nopa=0; 
}else{ 
$nopa=($Page-1)*$ShowNum; 
if($nopa<0){ 
$nopa=0; 
} 
} 
$maxNum=$this->num_rows($this->queryResult); 
$k=0; 
$i=0; 
$dd=$this->fetch_array($this->queryResult); 
while($dd&&$nopa<=$maxNum&&$i<$ShowNum){ 
if($nopa>=$maxNum)$nopa=$maxNum; 
mssql_data_seek($this->queryResult,$nopa); 
$row=$this->fetch_array($this->queryResult); 
$nopa++; 
$i++; 
$back[]=$row; 
if($nopa>=$maxNum){ 
break; 
} 
} 
} 
$this->pageNum=$maxNum; 
return$back; 
} 
/** 
* paging html The page number  
*/ 
functionpage_html($DataNum=0,$Page=1,$ShowNum=3,$web,$Post=''){ 
if($DataNum==0){ 
$back=" There is no data to query "; 
}else{ 
if($ShowNum<=0){ 
$ShowNum=3; 
} 
if($Page<=0){ 
$Page=1; 
} 
if(empty($web)){ 
$web="#"; 
} 
$pageNum=ceil($DataNum/$ShowNum); 
if($Page<=1){ 
$top=" Home page <<"; 
}else{ 
$top="<ahref='".$web."?page=0&".$Post."'target='_self'> Home page <<</a>"; 
} 
if($Page!==1){ 
$upPage="<ahref='".$web."?page=".($Page-1)."&".$Post."'target='_self'> on 1 page </a>"; 
}else{ 
$upPage=" on 1 page "; 
} 
if($Page<$pageNum){ 
$downPage="<ahref='".$web."?page=".($Page+1)."&".$Post."'target='_self'> Under the 1 page </a>"; 
}else{ 
$downPage=" Under the 1 page "; 
} 
if($Page==$pageNum){ 
$foot=">> back "; 
}else{ 
$foot="<ahref='".$web."?page=".$pageNum."&".$Post."'target='_self'>>> back </a>"; 
} 
$back=<<<EOT 
 A total of $pageNum page &nbsp;&nbsp; 
 The first $Page/$pageNum page $top&nbsp;$upPage&nbsp;$downPage&nbsp;$foot 
EOT; 
} 
return$back; 
} 
}//endclass 
?> 

Related articles: