PHP connection MSSQL method summary, phpmssql summary _ PHP Tutorial

Source: Internet
Author: User
Tags mssql client
PHP connection MSSQL method summary, phpmssql summary. PHP connection MSSQL method summary, phpmssql summary in order to allow PHP to connect MSSQL, the system needs to install MSSQL, PHP, and in PHP. in the configuration in ini, remove 1. summary of MSSQL methods connected to PHP and phpmssql

To allow PHP to connect to MSSQL, the system needs to install MSSQL and PHP. in the configuration of PHP. ini, remove the extension = php_mssql.dll;

1. connect to MSSQL

$ Conn = mssql_connect ("instance name or server IP", "username", "password"); // test connection if ($ conn) {echo "connection successful ";}

2. select the database to connect

 mssql_select_db("dbname");

3. execute the query

$ Rs = mssql_query ("select top 1 id, username from tbname", $ conn); // or directly execute the update, insert, and other statements, you do not need to assign mssql_query ("update tbname set username = 'niunv' where id = 1") to the returned result ");

4. get the number of record sets

 echo mssql_num_rows($rs);

5. obtain the record set

If ($ row = mssql_fetch_array ($ rs) {$ id = $ row [0]; // obtain the ID field value $ username = $ row [1]; // Obtain the username field value}

6. get the ID of the new record
Set the id field to the IDENTITY Field. after the insert statement is executed, a @ IDENTITY global variable value is generated. the queried result 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. release record set

 mssql_free_result($rs);

8. close the connection

 mssql_close($conn);  

Note: Using PHP to operate MSSQL is easier than connecting to MYSQL in ASP. Therefore, when MSSQL and MYSQL are required to coexist, it is easier to use PHP to connect MSSQL to operate the coexistence of MYSQL and MSSQL. if ASP is used to connect to MYSQL, you need to install a MYSQL driver. by default, ODBC in windows is not installed. sorry...

  • Install at least mssql client on the web server
  • Open php. ini and remove the semicolon before extension = php_mssql.dll.
  • Required: extension_dir
  • We recommend that you use php <= 4.0.9 <= 5.0.3. Currently, I have not successfully connected php 4.010 or 5.0.3.
  • You can go to phpe.net to obtain the corresponding class from the database connection page.

Below is a modified class

<? 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; // used for paging --- a total of several pieces of data var $ ER; /*** constructor **/function SQL ($ Server = '', $ UserName ='', $ PassWord = '', $ DataBase = '') {$ this-> server = $ Server; $ this-> userName = $ UserName; $ this-> passWord = $ PassWord; $ this-> dataBase = $ DataBase ;} /*** database connection **/fun Ction db_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 "; 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 ($ thi S-> linkID = 0) {$ this-> ER = "The database is not connected !! ";}$ This-> queryResult = mssql_query ($ Str); // $ this-> queryResult = mssql_query ($ Str, $ this-> linkID); if (! $ This-> queryResult) {$ this-> ER = "$ Str. The operation is not successful. query error !! "; Return 0; // ***************** for php 4.3.9 or a later version of the error with 1} return $ this-> queryResult ;} /*** Data Acquisition **/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 is obtained! "; Return 0;}/** public * function: Free the Query Result * success return 1 * failed: return 0 */function freeResult ($ result = "") {if ($ result! = "") $ This-> queryResult = $ result; return mssql_free_result ($ this-> queryResult );} /*** obtain the number of affected rows * obtain the number of rows in the operation **/function num_rows ($ result = "") {if ($ result! = "") {$ This-> queryResult = $ result; $ row = mssql_num_rows ($ this-> queryResult); return $ row ;}} /*** get query result --- 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 page ** $ Result database operation * str = SQL statement * page = page * showNum = page */fu Nction 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 <= $ ma XNum & $ 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 number */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 = "homepage <";} else {$ top = "homepage <";} 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 Page ";} else {$ foot = "> last page";} $ back = <
 

The above is a summary of the MSSQL method for PHP connection, and I hope it will help you learn it.

Articles you may be interested in:
  • Php adodb connects to mssql to solve the garbled problem
  • Php connection mssql database beginner php notes
  • Code for connecting to mssql2005 in php
  • Php connection mssql: pdo odbc SQL server
  • Some related experiences and precautions for connecting php to mssql
  • Several methods for connecting php to the mssql database
  • How to use the mssql extension compiled in Linux to connect to sqlserver2008 using php
  • PHP connection MSSQL2008/2005 database (SQLSRV) configuration instance
  • Php5.3 cannot connect to the mssql database

To allow PHP to connect to MSSQL, the system needs to install MSSQL and PHP. in the configuration of PHP. ini, replace extension = php_mssql.dll; remove 1. connect...

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.