php mssql 数据库连接类代码

php mssql 数据库连接类代码,代码如下:

  1. class DB_Sql {
  2. var $Host = "";
  3. var $Database = "";
  4. var $User = "";
  5. var $Password = "";
  6. var $Link_ID = 0;
  7. var $Query_ID = 0;
  8. var $Record = array();
  9. var $Row = 0;
  10. var $Errno = 0;
  11. var $Error = "";
  12. var $Auto_Free = 0; ## set this to 1 to automatically free results
  13. /* public: constructorwww.phpfensi.com */
  14. function DB_Sql($query = "") {
  15. $this->query($query);
  16. }
  17. function connect() {
  18. if ( 0 == $this->Link_ID ) {
  19. $this->Link_ID=mssql_connect($this->Host, $this->User, $this->Password);
  20. if (!$this->Link_ID)
  21. $this->halt("Link-ID == false, mssql_pconnect failed");
  22. else
  23. @mssql_select_db($this->Database, $this->Link_ID);
  24. }
  25. }
  26. function free_result(){
  27. mssql_free_result($this->Query_ID);
  28. $this->Query_ID = 0;
  29. }
  30. function query($Query_String)
  31. {
  32. /* No empty queries, please, since PHP4 chokes on them. */
  33. if ($Query_String == "")
  34. /* The empty query string is passed on from the constructor,
  35. * when calling the class without a query, e.g. in situations
  36. * like these: '$db = new DB_Sql_Subclass;'
  37. */
  38. return 0;
  39. if (!$this->Link_ID)
  40. $this->connect();
  41. # printf("<br>Debug: query = %s<br> ", $Query_String);
  42. $this->Query_ID = mssql_query($Query_String, $this->Link_ID);
  43. $this->Row = 0;
  44. if (!$this->Query_ID) {
  45. $this->Errno = 1;
  46. $this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
  47. $this->halt("Invalid SQL: ".$Query_String);
  48. }
  49. return $this->Query_ID;
  50. }
  51. function next_record() {
  52. if ($this->Record = mssql_fetch_row($this->Query_ID)) {
  53. // add to Record[<key>]
  54. $count = mssql_num_fields($this->Query_ID);
  55. for ($i=0; $i<$count; $i++){
  56. $fieldinfo = mssql_fetch_field($this->Query_ID,$i);
  57. $this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
  58. }
  59. $this->Row += 1;
  60. $stat = 1;
  61. } else {
  62. if ($this->Auto_Free) {
  63. $this->free_result();
  64. }
  65. $stat = 0;
  66. }
  67. return $stat;
  68. }
  69. function seek($pos) {
  70. mssql_data_seek($this->Query_ID,$pos);
  71. $this->Row = $pos;
  72. }
  73. function metadata($table) {
  74. $count = 0;
  75. $id = 0;
  76. $res = array();
  77. $this->connect();
  78. $id = mssql_query("select * from $table", $this->Link_ID);
  79. if (!$id) {
  80. $this->Errno = 1;
  81. $this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
  82. $this->halt("Metadata query failed.");
  83. }
  84. $count = mssql_num_fields($id);
  85. for ($i=0; $i<$count; $i++) {
  86. $info = mssql_fetch_field($id, $i);
  87. $res[$i]["table"] = $table;
  88. $res[$i]["name"] = $info["name"];
  89. $res[$i]["len"] = $info["max_length"];
  90. $res[$i]["flags"] = $info["numeric"];
  91. }
  92. $this->free_result();
  93. return $res;
  94. }
  95. function affected_rows() {
  96. // Not a supported function in PHP3/4. Chris Johnson, 16May2001.
  97. // return mssql_affected_rows($this->Query_ID);
  98. $rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID);
  99. if ($rsRows) {
  100. return mssql_result($rsRows, 0, "rows");
  101. }
  102. }
  103. function num_rows() {
  104. return mssql_num_rows($this->Query_ID);
  105. }
  106. function num_fields() {
  107. return mssql_num_fields($this->Query_ID);
  108. }
  109. function nf() {
  110. return $this->num_rows();
  111. }
  112. function np() {
  113. print $this->num_rows();
  114. }
  115. function f($Field_Name) {
  116. return $this->Record[strtolower($Field_Name)];
  117. }//开源代码phpfensi.com
  118. function p($Field_Name) {
  119. print $this->f($Field_Name);
  120. }
  121. function halt($msg) {
  122. printf("</td></tr></table><b>Database error:</b> %s<br> ", $msg);
  123. printf("<b>MSSQL Error</b>: %s (%s)<br> ",
  124. $this->Errno,
  125. $this->Error);
  126. die("Session halted.");
  127. }
  128. }