Some related experiences and summary of php connection to mssql

Source: Internet
Author: User
Tags mssql client
To allow PHP to connect to MSSQL, the system needs to install MSSQL and PHP. in the configuration in ini, remove 1. connect MSSQL $ conn = mssql_connect (& quot; instance name or server IP & quot;, & quot; Username & quot;, & quo to allow PHP to connect to MSSQL, the system needs to install MSSQL, PHP, and in PHP. in the configuration in ini, remove 1. connect MSSQL $ conn = mssql_connect ("instance name or server IP", "username", "password"); // test the connection if ($ conn) {echo "connection successful";} 2. select the database mssql_select_db ("dbname") to connect to; 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"); 4. obtain 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. obtain the ID of the new record and set the id field to the IDENTITY Field. after the insert statement is executed, a @ IDENTITY global variable value is generated. The last new record ID is queried. 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 // More http://www.52mvc.com Mssql_free_result ($ rs); 8. close mssql_close ($ conn); note: Using PHP to operate MSSQL is easier than connecting to MYSQL in ASP. Therefore, when MSSQL and MYSQL need to coexist, using PHP to connect to MSSQL to operate the coexistence of MYSQL and MSSQL is relatively simple and easy to use. if ASP is used to connect to MYSQL, you need to install a MYSQL driver. by default, ODBC in windows is not installed. sorry... 1. install at least the mssql client on the web server. open php. ini remove the semicolon before; extension = php_mssql.dll if necessary: extension_dir 3 needs to be formulated. 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 4. the connection page of the database can be obtained on phpe.net. Below is a class that I modified based on it. 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-> 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 = <
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.