PHP Connection MSSQL Method Rollup, phpmssql summary _php Tutorial

Source: Internet
Author: User

PHP Connection MSSQL Method Summary, Phpmssql Summary


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 = "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 = <<
 
  
 

The above is the PHP connection MSSQL method Summary, hope that everyone's learning has helped.

Articles you may be interested in:

    • PHP ADODB connection MSSQL solve garbled problem
    • PHP connection MSSQL Database beginner PHP notes
    • Code to connect mssql2005 under PHP
    • About PHP Connections mssql:pdo ODBC SQL Server
    • PHP connection mssql Some of the relevant experience and considerations
    • Several ways to connect MSSQL database with PHP
    • Linux compiled MSSQL extensions use PHP to connect sqlserver2008 using steps
    • PHP Connection mssql2008/2005 Database (SQLSRV) configuration instance
    • php5.3 Unable to connect MSSQL database solution

http://www.bkjia.com/PHPjc/1099078.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099078.html techarticle PHP Connection MSSQL Method Summary, Phpmssql Summary in order to allow PHP connection MSSQL, the system needs to install mssql,php, and in php.ini configuration, will extension=php_mssql.dll front; remove 1. Connection ...

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