How PHP connects to MSSQL

Source: Internet
Author: User
Tags sybase
MS SQL refers to Microsoft's SQL Server database servers, which is a database platform that provides a complete solution from the server to the end of the database, where the database server section is a database management system for establishing, using, and maintaining databases. SQL Server was not originally developed by Microsoft, but in order to compete with IBM, with Sybase, the first developer was Sybase, and Microsoft worked with Sybase on the development of SQL Server 4.2, and Microsoft Also Server 4.2 was ported to Windows NT (then version 3.1), and after terminating the partnership with Sybase, we developed SQL Server version 6.0, and the future SQL Server was developed by Microsoft itself.

This article is mainly for you to summarize the PHP connection MSSQL implementation method, to help you better realize the connection between PHP and MSSQL, interested in the small partners can refer to

In order to enable PHP to connect MSSQL, the system needs to install mssql,php, and in the php.ini configuration, will be Extension=php_mssql.dll front;

1. Connect MSSQL

  $conn =mssql_connect ("Instance name or server IP", "User name", "password");  Test Connection  if ($conn)  {  echo "connection succeeded";  }

2. Select the database to connect to

mssql_select_db ("dbname");

3. Execute the Query

$rs = Mssql_query ("Select top 1 id,username from Tbname", $conn);//or directly execute statements such as Update,insert, you can not assign a value to the returned result Mssql_query (" Update tbname set username= ' NIUNV ' where id=1 ');

4. Get the number of record set rows

echo mssql_num_rows ($RS);

5. Get a recordset

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.

  Mssql_query ("INSERT into tbname (username) VALUES (' NV ')", $conn);   $rs = Mssql_query ("SELECT @ @IDENTITY as ID", $conn);  if ($row =mssql_fetch_array ($rs))  {  echo $row [0];  }

7. Releasing Recordsets

Mssql_free_result ($RS);

8. Close the connection

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 ...

    • A client with at least MSSQL installed on the Web server

    • Open the php.ini and remove the semicolon in front of the Extension=php_mssql.dll.

    • It is necessary to develop extension_dir

    • Recommended use of php<=4.0.9 <=5.0.3 I have not yet connected successfully over 4.010 and 5.0.3

    • The connection page of the database can be obtained to the corresponding class on the phpe.net

Here is a class that I modified

 <?php/** *mssql Database connection class **/class sql{var $server;  var $userName;  var $passWord;   var $dataBase;  var $linkID = 0;  var $queryResult;   var $lastInsertID;   var $pageNum = 0;//Paging with---There are several data var $ER;  /** * constructor **/function SQL ($Server = ", $UserName =", $PassWord = ", $DataBase =") {$this->server = $Server;  $this->username = $UserName;  $this->password = $PassWord;  $this->database = $DataBase; }/** * Database connection **/function Db_connect () {$this->linkid = Mssql_pconnect ($this->server, $this->username, $this-&  Gt;password);  if (! $this->linkid) {$this->er = "Db_connect ($this->server, $this->username, $this->password) error";  return 0; } if (!mssql_select_db ($this->database, $this->linkid)) {$this->er = "mssql_select_db ($this->database,$  This->lastinsertid) Error ";  return 0;  } return $this->linkid; }/**public * Function:check the database, if exist then select * Exist:return 1 * not exist:return 0 */FunCtion Selectdatabase () {if (mssql_select_db ($this->database)) return 1;  else return 0; }/** * Data operation **/function query ($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, query error!!  ";  return 0;//**************** for PHP 4.3.9 or later error with 1} return $this->queryresult;  }/** * Data gets **/function Fetch_array ($result) {if ($result! = "") $this->queryresult = $result;   $rec =mssql_fetch_array ($this->queryresult);   if (Is_array ($rec)) {return $rec; }//$this->er = "No data obtained!"  ";  return 0; }/**public * Function:free the Query Result * Success return 1 * Failed:return 0 */function Freeresult ($resu  Lt= "") {if ($result! = "") $this->queryresult = $result;  Return Mssql_free_result ($this->queryresult); }/** * Gets the number of rows affected * Gets the number of rows that have been manipulated **/function num_rows ($result = "") {if ($result! = "") {$this->queryresult = $result;  $row = Mssql_num_rows ($this->queryresult);  return $row;  }}/** * Get query Results---multiple **/function result_ar ($str = ') {if (empty ($STR)) {return 0;  } $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 Statement *page = = Page *shownum = = Display several pages */function page ($Str, $Page =0, $ShowNum =5)  {$back = array ();//return data $maxNum = 0;  if ($Str = = "") {$this->er = "no data";  return 0;  } $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;  }/** * Page HTML page */function page_html ($DataNum =0, $Page =1, $ShowNum =3, $web, $Post = ") {if ($DataNum = = 0) {$back =  "No data to be queried";  }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 = "<a href= '". $web. "?  page=0& ". $Post." ' target= ' _self ' > Home << </a> "; if ($Page!==1) {$upPage = "<a href= '". $web. "? Page= ". ($Page-1). "  & ". $Post." ' target= ' _self ' > Prev </a> ";  }else {$upPage = "Previous page"; if ($Page < $pageNum) {$downPage = "<a href= '". $web. "? Page= ". ($Page + 1). "  & ". $Post." ' target= ' _self ' > Next </a> ";  }else {$downPage = "next page"; } IF ($Page = = $pageNum) {$foot = ">> Last"; }else {$foot = "<a href= '". $web. "? Page= ". $pageNum."  & ". $Post." ' target= ' _self ' > >> last </a> ';  } $back = <<<eot Total $pageNum page $Page/$pageNum page $top $upPage $downPage the $foot EOT;  } return $back; }}//end Class?>

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.