PHP connection to MSSQL some relevant experience and considerations _php Tutorial

Source: Internet
Author: User
In order to enable PHP to connect MSSQL, the system needs to install mssql,php, and in the php.ini configuration, will
Extension=php_mssql.dll in front of;
1. Connect MSSQL
Copy CodeThe code is as follows:
$conn =mssql_connect ("Instance name or server IP", "User name", "password");
Test connection
if ($conn)
{
echo "Connected successfully";
}

2. Select the database to connect to
Copy CodeThe code is as follows:
mssql_select_db ("dbname");

3. Execute the Query
Copy CodeThe code is as follows:
$rs =mssql_query ("Selecttop1id,usernamefromtbname", $conn);
Or you can directly execute Update,insert and other statements without assigning a value to the returned result.
Mssql_query ("Updatetbnamesetusername= ' NIUNV ' whereid=1");

4. Get the number of record set rows
Copy CodeThe code is as follows:
Echomssql_num_rows ($RS);

5. Get a Recordset
Copy CodeThe code is as follows:
if ($row =mssql_fetch_array ($RS))
{
$id = $row [0];//Gets the ID field value
$username = $row [1];//gets username field value
}

6. Get the ID of the new record
The ID field is set to the Identity field, and after the INSERT statement is executed, an @ @IDENTITY global variable value is generated, and the query is the ID of the last new record.
Copy CodeThe code is as follows:
Mssql_query ("Insertintotbname (username) VALUES (' NV ')", $conn);
$rs =mssql_query ("select@ @IDENTITYasid", $conn);
if ($row =mssql_fetch_array ($RS))
{
ECHO$ROW[0];
}

7. Releasing recordsets
Copy CodeThe code is as follows:
Mssql_free_result ($RS);

8. Close the connection
Copy CodeThe code is as follows:
Mssql_close ($conn);

Note: Using PHP to operate MSSQL than in the ASP connection MySQL to be simple, so, when the need for MSSQL and MySQL coexistence, using PHP to connect with MSSQL to operate MySQL and MSSQL is relatively simple and easy to use. If the ASP is connected to MySQL, Need to install a MySQL driver, default Windows ODBC is not installed, unfortunately ...
1. A client with at least MSSQL installed on the Web server
2. Open the php.ini and remove the semicolon in front of the Extension=php_mssql.dll.
It is necessary to develop extension_dir
3. Recommended use php<=4.0.9<=5.0.3 currently I have not connected successfully over 4.010 and 5.0.3
4. The connection page of the database can be obtained on the phpe.net to the corresponding class
Here's a class that I modified based on
Copy CodeThe code is as follows:
/**
*mssql Database Connection Class
**/
classsql{
Var$server;
Var$username;
Var$password;
Var$database;
Var$linkid=0;
Var$queryresult;
Var$lastinsertid;
var$pagenum=0;//pagination with---a total of several data
Var$er;
/**
* Constructor function
**/
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= "database is not connected!! ";
}
$this->queryresult=mssql_query ($STR);
$this->queryresult=mssql_query ($Str, $this->linkid);
if (! $this->queryresult) {
$this->er= "$Str. No operation succeeded, queryerror!! ";
return0;//**************** for errors above version php4.3.9 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= "not getting data! ";
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 operations
*str==sql statements
*page== first few pages
*shownum== display several pages
*/
Functionpage ($STR, $Page =0, $ShowNum =5) {
$back =array ();//Return Data
$maxNum = 0;
if ($Str = = "") {
$this->er= "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;
}
/**
* Pagination of HTML page numbers
*/
Functionpage_html ($DataNum =0, $Page =1, $ShowNum =3, $web, $Post = ') {
if ($DataNum ==0) {
$back = "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 <<";
}else{
$top = "Home <<";
}
if ($Page!==1) {
$upPage = "Previous page";
}else{
$upPage = "Previous page";
}
if ($Page < $pageNum) {
$downPage = "Next page";
}else{
$downPage = "Next page";
}
if ($Page = = $pageNum) {
$foot = ">> Last";
}else{
$foot = ">> Last";
}
$back =<<
Total $pagenum Pages
Page $page/$pageNum $top $upPage $downPage $foot
EOT;
}
Return$back;
}
}//endclass
?>

http://www.bkjia.com/PHPjc/326457.html www.bkjia.com true http://www.bkjia.com/PHPjc/326457.html techarticle In order to enable PHP to connect MSSQL, the system needs to install mssql,php, and in the configuration of php.ini, Extension=php_mssql.dll Front; remove 1. Connect MSSQL Copy code code as follows: $conn =mssql ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.