Summary of php connection to mssql

Source: Internet
Author: User
Tags mssql client
Summary of php connection to mssql

  1. $ Conn = mssql_connect ("instance name or server IP address", "username", "password ");
  2. // Test the connection
  3. If ($ conn)
  4. {
  5. Echo "connection successful ";
  6. }

2. select the database to connect

  1. Mssql_select_db ("dbname ");

3. query

  1. $ Rs = mssql_query ("select top 1 id, username from tbname", $ conn );
  2. Or directly execute the update, insert, and other statements without assigning values to the returned results.
  3. Mssql_query ("update tbname set username = 'niunv' where id = 1 ");

4. obtain the number of record sets

  1. Echo mssql_num_rows ($ rs );

5. obtain the record set

  1. If ($ row = mssql_fetch_array ($ rs ))
  2. {
  3. $ Id = $ row [0]; // obtain the ID field value
  4. $ Username = $ row [1]; // obtain the value of the username field.
  5. }

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 query result shows the ID of the last new record.

  1. Mssql_query ("insert into tbname (username) values ('nv ')", $ conn );
  2. $ Rs = mssql_query ("select @ IDENTITY as id", $ conn );
  3. If ($ row = mssql_fetch_array ($ rs ))
  4. {
  5. Echo $ row [0];
  6. }

7. release record set // More

  1. Mssql_free_result ($ rs );

8. close the connection

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

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.

  1. /**
  2. * Mssql database connection class
  3. **/
  4. Class SQL {
  5. Var $ server;
  6. Var $ userName;
  7. Var $ passWord;
  8. Var $ dataBase;
  9. Var $ linkID = 0;
  10. Var $ queryResult;
  11. Var $ lastInsertID;
  12. Var $ pageNum = 0; // use for paging --- a total of several data records
  13. Var $ ER;
  14. /**
  15. * Constructor
  16. **/
  17. Function SQL ($ Server = '', $ UserName ='', $ PassWord = '', $ DataBase = ''){
  18. $ This-> server = $ Server;
  19. $ This-> userName = $ UserName;
  20. $ This-> passWord = $ PassWord;
  21. $ This-> dataBase = $ DataBase;
  22. }
  23. /**
  24. * Database connection
  25. **/
  26. Function db_connect (){
  27. $ This-> linkID = mssql_pconnect ($ this-> server, $ this-> userName, $ this-> passWord );
  28. If (! $ This-> linkID ){
  29. $ This-> ER = "db_connect ($ this-> server, $ this-> userName, $ this-> passWord) error ";
  30. Return 0;
  31. }
  32. If (! Mssql_select_db ($ this-> dataBase, $ this-> linkID )){
  33. $ This-> ER = "mssql_select_db ($ this-> dataBase, $ this-> lastInsertID) error ";
  34. Return 0;
  35. }
  36. Return $ this-> linkID;
  37. }
  38. /** Public
  39. * Function: Check the database, if exist then select
  40. * Exist: return 1
  41. * Not exist: return 0
  42. */
  43. Function selectDatabase (){
  44. If (mssql_select_db ($ this-> dataBase ))
  45. Return 1;
  46. Else
  47. Return 0;
  48. }
  49. /**
  50. * Data operations
  51. **/
  52. Function query ($ Str ){
  53. If ($ this-> linkID = 0 ){
  54. $ This-> ER = "The database is not connected !! ";
  55. }
  56. $ This-> queryResult = mssql_query ($ Str );
  57. // $ This-> queryResult = mssql_query ($ Str, $ this-> linkID );
  58. If (! $ This-> queryResult ){
  59. $ This-> ER = "$ Str. no operation is successful. query error !! ";
  60. Return 0; // ***************** for php 4.3.9 or a later version of Error 1
  61. }
  62. Return $ this-> queryResult;
  63. }
  64. /**
  65. * Data acquisition
  66. **/
  67. Function fetch_array ($ result ){
  68. If ($ result! = "") $ This-> queryResult = $ result;
  69. $ Rec = mssql_fetch_array ($ this-> queryResult );
  70. If (is_array ($ rec )){
  71. Return $ rec;
  72. }
  73. // $ This-> ER = "no data is obtained! ";
  74. Return 0;
  75. }
  76. /** Public
  77. * Function: Free the Query Result
  78. * Success return 1
  79. * Failed: return 0
  80. */
  81. Function freeResult ($ result = ""){
  82. If ($ result! = "") $ This-> queryResult = $ result;
  83. Return mssql_free_result ($ this-> queryResult );
  84. }
  85. /**
  86. * Obtain the number of affected rows.
  87. * Get the number of rows that have been operated
  88. **/
  89. Function num_rows ($ result = ""){
  90. If ($ result! = ""){
  91. $ This-> queryResult = $ result;
  92. $ Row = mssql_num_rows ($ this-> queryResult );
  93. Return $ row;
  94. }
  95. }
  96. /**
  97. * Retrieve query results --- multiple
  98. **/
  99. Function result_ar ($ str = ''){
  100. If (empty ($ str )){
  101. Return 0;
  102. }
  103. $ Back = array ();
  104. $ This-> queryResult = $ this-> query ($ str );
  105. While ($ row = $ this-> fetch_array ($ this-> queryResult )){
  106. $ Back [] = $ row;
  107. }
  108. Return $ back;
  109. }
  110. /**
  111. * Database information paging
  112. * $ Result database operations
  113. * Str = SQL statement
  114. * Page = page number
  115. * ShowNum = Display pages
  116. */
  117. Function page ($ Str, $ Page = 0, $ ShowNum = 5 ){
  118. $ Back = array (); // return data
  119. $ MaxNum = 0;
  120. If ($ Str = ""){
  121. $ This-> ER = "no data ";
  122. Return 0;
  123. }
  124. $ This-> queryResult = $ this-> query ($ Str );
  125. If ($ this-> queryResult ){
  126. If ($ Page = ""){
  127. $ Nopa = 0;
  128. } Else {
  129. $ Nopa = ($ Page-1) * $ ShowNum;
  130. If ($ nopa <0 ){
  131. $ Nopa = 0;
  132. }
  133. }
  134. $ MaxNum = $ this-> num_rows ($ this-> queryResult );
  135. $ K = 0;
  136. $ I = 0;
  137. $ Dd = $ this-> fetch_array ($ this-> queryResult );
  138. While ($ dd & $ nopa <= $ maxNum & $ I <$ ShowNum ){
  139. If ($ nopa >=$ maxNum) $ nopa = $ maxNum;
  140. Mssql_data_seek ($ this-> queryResult, $ nopa );
  141. $ Row = $ this-> fetch_array ($ this-> queryResult );
  142. $ Nopa ++;
  143. $ I ++;
  144. $ Back [] = $ row;
  145. If ($ nopa >=$ maxNum ){
  146. Break;
  147. }
  148. }
  149. }
  150. $ This-> pageNum = $ maxNum;
  151. Return $ back;
  152. }
  153. /**
  154. * Page number of the html page
  155. */
  156. Function page_html ($ DataNum = 0, $ Page = 1, $ ShowNum = 3, $ web, $ Post = ''){
  157. If ($ DataNum = 0 ){
  158. $ Back = "no data to be queried ";
  159. } Else {
  160. If ($ ShowNum <= 0 ){
  161. $ ShowNum = 3;
  162. }
  163. If ($ Page <= 0 ){
  164. $ Page = 1;
  165. }
  166. If (empty ($ web )){
  167. $ Web = "#";
  168. }
  169. $ PageNum = ceil ($ DataNum/$ ShowNum );
  170. If ($ Page <= 1 ){
  171. $ Top = "homepage <";
  172. } Else {
  173. $ Top = "homepage <";
  174. }
  175. If ($ Page! = 1 ){
  176. $ UpPage = "previous page ";
  177. } Else {
  178. $ UpPage = "previous page ";
  179. }
  180. If ($ Page <$ pageNum ){
  181. $ DownPage = "next page ";
  182. } Else {
  183. $ DownPage = "next page ";
  184. }
  185. If ($ Page = $ pageNum ){
  186. $ Foot = "> last page ";
  187. } Else {
  188. $ Foot = "> last page ";
  189. }
  190. $ Back = <
  191. Total $ pageNum pages
  192. Page $ Page/$ pageNum Page $ top $ upPage $ downPage $ foot
  193. EOT;
  194. }
  195. Return $ back;
  196. }
  197. } // End class
  198. ?>

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.