Source for file db-odbc.php

Documentation is available at db-odbc.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: db-odbc.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for ODBC database access. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package database */* ODBC database interface
  27. * This is a database interface class. It is an impedance-matcher
  28. * between the high-level Phplib functions for accessing data, and
  29. * the specific functions suplpied by Php to access a particular
  30. * flavour of databse such as Postgres, MS-SQL Server, Sybase etc.
  31. * @package database
  32. * @access private
  33. */
  34. class db_odbc extends database {
  35. /** Constructor */
  36.  
  37. function db_odbc($name="", $user="", $passwd="", $host="", $port=0) {
  38. $this->database($name, $user, $passwd, $host, $port);
  39. $this->type = "odbc";
  40. }
  41. // ....................................................................
  42. /**
  43. * Connect to the database.
  44. * @param boolean $persistent Whether to connect persistently or not
  45. * @return boolean Status true if connected successfully
  46. */
  47. function connect($persistent=NOT_PERSISTENT) {
  48. if (!$this->connected) {
  49. $dsn = "";
  50. $server = "";
  51. if ($this->host != "") $server .= $this->host;
  52. if ($this->port != 0) $server .= ":" . $this->port;
  53. if ($server != "") $dsn .= "SERVER=$server;";
  54. $dsn .= "DATABASE=" . $this->name . ";";
  55. $dsn = trim($connstr);
  56. if ($persistent)
  57. $this->dbid = odbc_pconnect($dsn, $this->user, $this->passwd, SQL_CUR_USE_ODBC);
  58. else
  59. $this->dbid = odbc_connect($dsn, $this->user, $this->passwd, SQL_CUR_USE_ODBC);
  60. if ($this->dbid) {
  61. $this->connected = true;
  62. }
  63. }
  64. return $this->connected;
  65. }
  66. // ....................................................................
  67. /** Disconnect from the database, if connected. */
  68.  
  69. function disconnect() {
  70. if (odbc_close($this->dbid)) {
  71. $this->connected = false;
  72. }
  73. }
  74. // ....................................................................
  75. /**
  76. * Execute a query on the connected database.
  77. * @param string $sql The SQL query to execute on the database
  78. * @return resource A database query resource ID, or false if query failed
  79. */
  80. function query($sql) {
  81. $sql = $this->convert_boolean_syntax($sql);
  82. $this->timer->restart();
  83. $rid = odbc_exec($this->dbid, $sql);
  84. $this->timer->stop();
  85. $this->executable_sql = $sql;
  86. $this->rid = $rid;
  87. $this->query_report();
  88. return $rid;
  89. }
  90. // ....................................................................
  91. /**
  92. * Return the number of rows returned by a SELECT query.
  93. * @param resource $rid The resource ID for the executed query
  94. * @return integer The number of rows returned by the query
  95. */
  96. function numrows($rid) {
  97. return odbc_num_rows($rid);
  98. }
  99. // ....................................................................
  100. /**
  101. * Return the number of rows affected by a query.
  102. * @param resource $rid The resource ID for the executed query
  103. * @return integer The number of rows affected by the query
  104. */
  105. function affectedrows($rid) {
  106. return odbc_num_rows($rid);
  107. }
  108. // ....................................................................
  109. /**
  110. * Free a resource.
  111. * @param resource $rid The resource ID for the executed query
  112. */
  113. function freeresult($rid) {
  114. odbc_free_result($rid);
  115. }
  116. // ....................................................................
  117. /**
  118. * Return the last error message.
  119. * @return string The last error message which was generated
  120. */
  121. function errormessage() {
  122. return odbc_errormsg($this->dbid);
  123. }
  124. // ....................................................................
  125. /**
  126. * Return the specified row, as a standard (enumerated) array of
  127. * field values.
  128. * @param resource $rid The resource ID for the executed query
  129. * @param integer $rowno Row number (zero-based) of row to return
  130. * @return array Enumerated array of field values
  131. */
  132. function fetch_row($rid, $rowno) {
  133. return odbc_fetch_row($rid, $rowno);
  134. }
  135. // ....................................................................
  136. /**
  137. * Return the specified row, as an associative array of fields
  138. * in a fieldname => value format.
  139. * @param resource $rid The resource ID for the executed query
  140. * @param integer $rowno Row number (zero-based) of row to return
  141. * @return array Associative array of field values
  142. */
  143. function fetch_array($rid, $rowno) {
  144. if(odbc_fetch_into($rid, $rowno, $ret)) {
  145. while (list($key, $value) = @each($ret)) {
  146. $arret[odbc_field_name($res, $key+1)] = $value;
  147. }
  148. return $arret;
  149. } else return false;
  150. }
  151. }
  152.  
  153. // ----------------------------------------------------------------------
  154. // Ensure ODBC Php module is present..
  155.  
  156. if (!extension_loaded("odbc")) {
  157. if (!dl("odbc.so")) {
  158. exit;
  159. }
  160. }
  161. // ----------------------------------------------------------------------
  162. ?>

Documentation generated by phpDocumentor 1.3.0RC3