Home >Backend Development >PHP Tutorial >Some summaries about php connecting to mssql

Some summaries about php connecting to mssql

WBOY
WBOYOriginal
2016-07-25 09:04:34970browse
  1. $conn=mssql_connect("Instance name or server IP","Username","Password");
  2. //Test the connection
  3. if($conn)
  4. {
  5. echo "Connection successful";
  6. }
Copy code

2. Select the database to connect to

  1. mssql_select_db("dbname");
Copy code

3. Execute query

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

4. Get the number of rows in the record set

  1. echo mssql_num_rows($rs);
Copy code

5. Get the record set

  1. if($row=mssql_fetch_array($rs))
  2. {
  3. $id = $row[0];//Get the ID field value
  4. $username = $row[1];//Get the username field Value
  5. }
Copy code

6. Get the ID of the new record Set the id field to the IDENTITY field, and after executing the insert statement, a @@IDENTITY global variable value will be generated, and the query will be the ID of the last newly added 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. }
Copy code

7. Release the record set //More

  1. mssql_free_result($rs);
Copy code

8. Close the connection

  1. mssql_close($conn);
Copy code

Note: It is easier to use PHP to operate MSSQL than to connect to MYSQL in ASP. Therefore, when MSSQL and MYSQL need to coexist, it is easier and easier to use PHP to connect to MSSQL to operate MYSQL and MSSQL. If ASP is to connect to MYSQL, you need to install a MYSQL driver. , the default windows ODBC is not installed, unfortunately...

1. Install at least mssql client on the web server 2. Open php.ini and remove the semicolon in front of;extension=php_mssql.dll If necessary: ​​extension_dir needs to be specified 3. It is recommended to use php

  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;//分页用---共有几条数据
  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 operation
  51. **/
  52. function query($Str){
  53. if ($this->linkID == 0) {
  54. $this->ER = "数据库还没有连接!!";
  55. }
  56. $this->queryResult = mssql_query($Str);
  57. //$this->queryResult = mssql_query($Str,$this->linkID);
  58. if (!$this->queryResult) {
  59. $this->ER = "$Str.没有操作成功,query error!!";
  60. return 0;//********************For errors in PHP 4.3.9 and above, use 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 = "没有获取数据!";
  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. *Get the number of affected rows
  87. *Get the number of operated rows
  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. *Get 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 operation
  113. *str ==sql statement
  114. *page ==Which page
  115. *showNum ==How many pages to display
  116. */
  117. function page($Str,$Page=0,$ShowNum=5){
  118. $back = array();//返回数据
  119. $maxNum = 0;
  120. if ($Str == "") {
  121. $this->ER = "没有数据";
  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 html page number
  155. */
  156. function page_html($DataNum=0,$Page=1,$ShowNum=3,$web,$Post=''){
  157. if ($DataNum == 0) {
  158. $back = "没有要查询的数据";
  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 = "首页<<";
  172. }else {
  173. $top = "首页<< ";
  174. }
  175. if ($Page !==1) {
  176. $upPage = "上一页";
  177. }else {
  178. $upPage = "上一页";
  179. }
  180. if ($Page < $pageNum) {
  181. $downPage = "下一页";
  182. }else {
  183. $downPage = "下一页";
  184. }
  185. if ($Page == $pageNum) {
  186. $foot = ">>尾页";
  187. }else {
  188. $foot = " >>尾页";
  189. }
  190. $back = <<
  191. 共 $pageNum 页
  192. 第 $Page/$pageNum 页 $top $upPage $downPage $foot
  193. EOT;
  194. }
  195. return $back;
  196. }
  197. }//end class
  198. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn