msql.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Sterling Hughes <sterling@php.net> |
  17. // | Maintainer: Daniel Convissor <danielc@php.net> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: msql.php,v 1.22 2004/02/18 05:37:31 danielc Exp $
  21. require_once 'DB/common.php';
  22. /**
  23. * Database independent query interface definition for PHP's Mini-SQL
  24. * extension.
  25. *
  26. * @package DB
  27. * @version $Id: msql.php,v 1.22 2004/02/18 05:37:31 danielc Exp $
  28. * @category Database
  29. * @author Sterling Hughes <sterling@php.net>
  30. */
  31. class DB_msql extends DB_common
  32. {
  33. // {{{ properties
  34. var $connection;
  35. var $phptype, $dbsyntax;
  36. var $prepare_tokens = array();
  37. var $prepare_types = array();
  38. // }}}
  39. // {{{ constructor
  40. function DB_msql()
  41. {
  42. $this->DB_common();
  43. $this->phptype = 'msql';
  44. $this->dbsyntax = 'msql';
  45. $this->features = array(
  46. 'prepare' => false,
  47. 'pconnect' => true,
  48. 'transactions' => false,
  49. 'limit' => 'emulate'
  50. );
  51. }
  52. // }}}
  53. // {{{ connect()
  54. function connect($dsninfo, $persistent = false)
  55. {
  56. if (!DB::assertExtension('msql')) {
  57. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  58. }
  59. $this->dsn = $dsninfo;
  60. $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  61. $connect_function = $persistent ? 'msql_pconnect' : 'msql_connect';
  62. if ($dbhost && $dsninfo['username'] && $dsninfo['password']) {
  63. $conn = $connect_function($dbhost, $dsninfo['username'],
  64. $dsninfo['password']);
  65. } elseif ($dbhost && $dsninfo['username']) {
  66. $conn = $connect_function($dbhost, $dsninfo['username']);
  67. } else {
  68. $conn = $connect_function($dbhost);
  69. }
  70. if (!$conn) {
  71. $this->raiseError(DB_ERROR_CONNECT_FAILED);
  72. }
  73. if (!@msql_select_db($dsninfo['database'], $conn)){
  74. return $this->raiseError(DB_ERROR_NODBSELECTED);
  75. }
  76. $this->connection = $conn;
  77. return DB_OK;
  78. }
  79. // }}}
  80. // {{{ disconnect()
  81. function disconnect()
  82. {
  83. $ret = @msql_close($this->connection);
  84. $this->connection = null;
  85. return $ret;
  86. }
  87. // }}}
  88. // {{{ simpleQuery()
  89. function simpleQuery($query)
  90. {
  91. $this->last_query = $query;
  92. $query = $this->modifyQuery($query);
  93. $result = @msql_query($query, $this->connection);
  94. if (!$result) {
  95. return $this->raiseError();
  96. }
  97. // Determine which queries that should return data, and which
  98. // should return an error code only.
  99. return DB::isManip($query) ? DB_OK : $result;
  100. }
  101. // }}}
  102. // {{{ nextResult()
  103. /**
  104. * Move the internal msql result pointer to the next available result
  105. *
  106. * @param a valid fbsql result resource
  107. *
  108. * @access public
  109. *
  110. * @return true if a result is available otherwise return false
  111. */
  112. function nextResult($result)
  113. {
  114. return false;
  115. }
  116. // }}}
  117. // {{{ fetchInto()
  118. /**
  119. * Fetch a row and insert the data into an existing array.
  120. *
  121. * Formating of the array and the data therein are configurable.
  122. * See DB_result::fetchInto() for more information.
  123. *
  124. * @param resource $result query result identifier
  125. * @param array $arr (reference) array where data from the row
  126. * should be placed
  127. * @param int $fetchmode how the resulting array should be indexed
  128. * @param int $rownum the row number to fetch
  129. *
  130. * @return mixed DB_OK on success, NULL when end of result set is
  131. * reached or on failure
  132. *
  133. * @see DB_result::fetchInto()
  134. * @access private
  135. */
  136. function fetchInto($result, &$arr, $fetchmode, $rownum=null)
  137. {
  138. if ($rownum !== null) {
  139. if (!@msql_data_seek($result, $rownum)) {
  140. return null;
  141. }
  142. }
  143. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  144. $arr = @msql_fetch_array($result, MSQL_ASSOC);
  145. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  146. $arr = array_change_key_case($arr, CASE_LOWER);
  147. }
  148. } else {
  149. $arr = @msql_fetch_row($result);
  150. }
  151. if (!$arr) {
  152. if ($error = msql_error()) {
  153. return $this->raiseError($error);
  154. } else {
  155. return null;
  156. }
  157. }
  158. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  159. $this->_rtrimArrayValues($arr);
  160. }
  161. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  162. $this->_convertNullArrayValuesToEmpty($arr);
  163. }
  164. return DB_OK;
  165. }
  166. // }}}
  167. // {{{ freeResult()
  168. function freeResult($result)
  169. {
  170. return @msql_free_result($result);
  171. }
  172. // }}}
  173. // {{{ numCols()
  174. function numCols($result)
  175. {
  176. $cols = @msql_num_fields($result);
  177. if (!$cols) {
  178. return $this->raiseError();
  179. }
  180. return $cols;
  181. }
  182. // }}}
  183. // {{{ numRows()
  184. function numRows($result)
  185. {
  186. $rows = @msql_num_rows($result);
  187. if (!$rows) {
  188. return $this->raiseError();
  189. }
  190. return $rows;
  191. }
  192. // }}}
  193. // {{{ affected()
  194. /**
  195. * Gets the number of rows affected by a query.
  196. *
  197. * @return number of rows affected by the last query
  198. */
  199. function affectedRows()
  200. {
  201. return @msql_affected_rows($this->connection);
  202. }
  203. // }}}
  204. }
  205. /*
  206. * Local variables:
  207. * tab-width: 4
  208. * c-basic-offset: 4
  209. * End:
  210. */
  211. ?>