PDOStatement_mysql.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /** File PDOStatement_mysql.class.php *
  3. *(C) Andrea Giammarchi [2005/10/13] */
  4. /**
  5. * Class PDOStatement_mysql
  6. * This class is used from class PDO_mysql to manage a MySQL database.
  7. * Look at PDO.clas.php file comments to know more about MySQL connection.
  8. * ---------------------------------------------
  9. * @Compatibility >= PHP 4
  10. * @Dependencies PDO.class.php
  11. * PDO_mysql.class.php
  12. * @Author Andrea Giammarchi
  13. * @Site http://www.devpro.it/
  14. * @Mail andrea [ at ] 3site [ dot ] it
  15. * @Date 2005/10/13
  16. * @LastModified 2006/01/29 09:30 [fixed execute bug]
  17. * @Version 0.1b - tested
  18. */
  19. class PDOStatement_mysql {
  20. /**
  21. * 'Private' variables:
  22. * __connection:Resource Database connection
  23. * __dbinfo:Array Array with 4 elements used to manage connection
  24. * __persistent:Boolean Connection mode, is true on persistent, false on normal (deafult) connection
  25. * __query:String Last query used
  26. * __result:Resource Last result from last query
  27. * __fetchmode:Integer constant PDO_FETCH_* result mode
  28. * __errorCode:String Last error string code
  29. * __errorInfo:Array Last error informations, code, number, details
  30. * __boundParams:Array Stored bindParam
  31. */
  32. var $__connection;
  33. var $__dbinfo;
  34. var $__persistent = false;
  35. var $__query = '';
  36. var $__result = null;
  37. var $__fetchmode = PDO::FETCH_BOTH;
  38. var $__errorCode = '';
  39. var $__errorInfo = Array('');
  40. var $__boundParams = Array();
  41. /**
  42. * Public constructor:
  43. * Called from PDO to create a PDOStatement for this database
  44. * new PDOStatement_sqlite( &$__query:String, &$__connection:Resource, $__dbinfo:Array )
  45. * @Param String query to prepare
  46. * @Param Resource database connection
  47. * @Param Array 4 elements array to manage connection
  48. */
  49. function PDOStatement_mysql(&$__query, &$__connection, &$__dbinfo) {
  50. $this->__query = &$__query;
  51. $this->__connection = &$__connection;
  52. $this->__dbinfo = &$__dbinfo;
  53. }
  54. /**
  55. * Public method:
  56. * Replace ? or :named values to execute prepared query
  57. * this->bindParam( $mixed:Mixed, &$variable:Mixed, $type:Integer, $lenght:Integer ):Void
  58. * @Param Mixed Integer or String to replace prepared value
  59. * @Param Mixed variable to replace
  60. * @Param Integer this variable is not used but respects PDO original accepted parameters
  61. * @Param Integer this variable is not used but respects PDO original accepted parameters
  62. */
  63. function bindParam($mixed, &$variable, $type = null, $lenght = null) {
  64. if(is_string($mixed))
  65. $this->__boundParams[$mixed] = $variable;
  66. else
  67. array_push($this->__boundParams, $variable);
  68. }
  69. /**
  70. * Public method:
  71. * Checks if query was valid and returns how may fields returns
  72. * this->columnCount( void ):Void
  73. */
  74. function columnCount() {
  75. $result = 0;
  76. if(!is_null($this->__result))
  77. $result = mysql_num_fields($this->__result);
  78. return $result;
  79. }
  80. /**
  81. * Public method:
  82. * Returns a code rappresentation of an error
  83. * this->errorCode( void ):String
  84. * @Return String String rappresentation of the error
  85. */
  86. function errorCode() {
  87. return $this->__errorCode;
  88. }
  89. /**
  90. * Public method:
  91. * Returns an array with error informations
  92. * this->errorInfo( void ):Array
  93. * @Return Array Array with 3 keys:
  94. * 0 => error code
  95. * 1 => error number
  96. * 2 => error string
  97. */
  98. function errorInfo() {
  99. return $this->__errorInfo;
  100. }
  101. /**
  102. * Public method:
  103. * Excecutes a query and returns true on success or false.
  104. * this->exec( $array:Array ):Boolean
  105. * @Param Array If present, it should contain all replacements for prepared query
  106. * @Return Boolean true if query has been done without errors, false otherwise
  107. */
  108. function execute($array = Array()) {
  109. if(count($this->__boundParams) > 0)
  110. $array = &$this->__boundParams;
  111. $__query = $this->__query;
  112. if(count($array) > 0) {
  113. foreach($array as $k => $v) {
  114. if(!is_int($k) || substr($k, 0, 1) === ':') {
  115. if(!isset($tempf))
  116. $tempf = $tempr = array();
  117. array_push($tempf, $k);
  118. array_push($tempr, '"'.mysql_escape_string($v).'"');
  119. }
  120. else {
  121. $parse = create_function('$v', 'return \'"\'.mysql_escape_string($v).\'"\';');
  122. $__query = preg_replace("/(\?)/e", '$parse($array[$k++]);', $__query);
  123. break;
  124. }
  125. }
  126. if(isset($tempf)) {
  127. foreach ($tempf as $k=>$v) {
  128. $search[$k] = '/' . preg_quote($tempf[$k],'`') . '\b/';
  129. }
  130. $__query = preg_replace($search, $tempr, $__query);
  131. //$__query = str_replace($tempf, $tempr, $__query);
  132. }
  133. }
  134. if(is_null($this->__result = &$this->__uquery($__query)))
  135. $keyvars = false;
  136. else
  137. $keyvars = true;
  138. $this->__boundParams = array();
  139. return $keyvars;
  140. }
  141. /**
  142. * Public method:
  143. * Returns, if present, next row of executed query or false.
  144. * this->fetch( $mode:Integer, $cursor:Integer, $offset:Integer ):Mixed
  145. * @Param Integer PDO_FETCH_* constant to know how to read next row, default PDO_FETCH_BOTH
  146. * NOTE: if $mode is omitted is used default setted mode, PDO_FETCH_BOTH
  147. * @Param Integer this variable is not used but respects PDO original accepted parameters
  148. * @Param Integer this variable is not used but respects PDO original accepted parameters
  149. * @Return Mixed Next row of executed query or false if there is nomore.
  150. */
  151. function fetch($mode = PDO_FETCH_ASSOC, $cursor = null, $offset = null) {
  152. if(func_num_args() == 0)
  153. $mode = &$this->__fetchmode;
  154. $result = false;
  155. if(!is_null($this->__result)) {
  156. switch($mode) {
  157. case PDO::FETCH_NUM:
  158. $result = mysql_fetch_row($this->__result);
  159. break;
  160. case PDO::FETCH_ASSOC:
  161. $result = mysql_fetch_assoc($this->__result);
  162. break;
  163. case PDO::FETCH_OBJ:
  164. $result = mysql_fetch_object($this->__result);
  165. break;
  166. case PDO::FETCH_BOTH:
  167. default:
  168. $result = mysql_fetch_array($this->__result);
  169. break;
  170. }
  171. }
  172. if(!$result)
  173. $this->__result = null;
  174. return $result;
  175. }
  176. /**
  177. * Public method:
  178. * Returns an array with all rows of executed query.
  179. * this->fetchAll( $mode:Integer ):Array
  180. * @Param Integer PDO_FETCH_* constant to know how to read all rows, default PDO_FETCH_BOTH
  181. * NOTE: this doesn't work as fetch method, then it will use always PDO_FETCH_BOTH
  182. * if this param is omitted
  183. * @Return Array An array with all fetched rows
  184. */
  185. function fetchAll($mode = PDO_FETCH_ASSOC) {
  186. $result = array();
  187. if(!is_null($this->__result)) {
  188. switch($mode) {
  189. case PDO::FETCH_NUM:
  190. while($r = mysql_fetch_row($this->__result))
  191. array_push($result, $r);
  192. break;
  193. case PDO::FETCH_ASSOC:
  194. while($r = mysql_fetch_assoc($this->__result))
  195. array_push($result, $r);
  196. break;
  197. case PDO::FETCH_OBJ:
  198. while($r = mysql_fetch_object($this->__result))
  199. array_push($result, $r);
  200. break;
  201. case PDO::FETCH_BOTH:
  202. default:
  203. while($r = mysql_fetch_array($this->__result))
  204. array_push($result, $r);
  205. break;
  206. }
  207. }
  208. $this->__result = null;
  209. return $result;
  210. }
  211. /**
  212. * Public method:
  213. * Returns, if present, first column of next row of executed query
  214. * this->fetchSingle( void ):Mixed
  215. * @Return Mixed Null or next row's first column
  216. */
  217. function fetchSingle() {
  218. $result = null;
  219. if(!is_null($this->__result)) {
  220. $result = @mysql_fetch_row($this->__result);
  221. if($result)
  222. $result = $result[0];
  223. else
  224. $this->__result = null;
  225. }
  226. return $result;
  227. }
  228. function fetchColumn($column=0) {
  229. $row = mysql_fetch_row($this->__result);
  230. return $row[$column];
  231. }
  232. /**
  233. * Public method:
  234. * Returns number of last affected database rows
  235. * this->rowCount( void ):Integer
  236. * @Return Integer number of last affected rows
  237. * NOTE: works with INSERT, UPDATE and DELETE query type
  238. */
  239. function rowCount() {
  240. return mysql_affected_rows($this->__connection);
  241. }
  242. // NOT TOTALLY SUPPORTED PUBLIC METHODS
  243. /**
  244. * Public method:
  245. * Quotes correctly a string for this database
  246. * this->getAttribute( $attribute:Integer ):Mixed
  247. * @Param Integer a constant [ PDO_ATTR_SERVER_INFO,
  248. * PDO_ATTR_SERVER_VERSION,
  249. * PDO_ATTR_CLIENT_VERSION,
  250. * PDO_ATTR_PERSISTENT ]
  251. * @Return Mixed correct information or false
  252. */
  253. function getAttribute($attribute) {
  254. $result = false;
  255. switch($attribute) {
  256. case PDO_ATTR_SERVER_INFO:
  257. $result = mysql_get_host_info($this->__connection);
  258. break;
  259. case PDO_ATTR_SERVER_VERSION:
  260. $result = mysql_get_server_info($this->__connection);
  261. break;
  262. case PDO_ATTR_CLIENT_VERSION:
  263. $result = mysql_get_client_info();
  264. break;
  265. case PDO_ATTR_PERSISTENT:
  266. $result = $this->__persistent;
  267. break;
  268. }
  269. return $result;
  270. }
  271. /**
  272. * Public method:
  273. * Sets database attributes, in this version only connection mode.
  274. * this->setAttribute( $attribute:Integer, $mixed:Mixed ):Boolean
  275. * @Param Integer PDO_* constant, in this case only PDO_ATTR_PERSISTENT
  276. * @Param Mixed value for PDO_* constant, in this case a Boolean value
  277. * true for permanent connection, false for default not permament connection
  278. * @Return Boolean true on change, false otherwise
  279. */
  280. function setAttribute($attribute, $mixed) {
  281. $result = false;
  282. if($attribute === PDO_ATTR_PERSISTENT && $mixed != $this->__persistent) {
  283. $result = true;
  284. $this->__persistent = (boolean) $mixed;
  285. mysql_close($this->__connection);
  286. if($this->__persistent === true)
  287. $this->__connection = &mysql_pconnect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  288. else
  289. $this->__connection = &mysql_connect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  290. mysql_select_db($this->__dbinfo[3], $this->__connection);
  291. }
  292. return $result;
  293. }
  294. /**
  295. * Public method:
  296. * Sets default fetch mode to use with this->fetch() method.
  297. * this->setFetchMode( $mode:Integer ):Boolean
  298. * @Param Integer PDO_FETCH_* constant to use while reading an execute query with fetch() method.
  299. * NOTE: PDO_FETCH_LAZY and PDO_FETCH_BOUND are not supported
  300. * @Return Boolean true on change, false otherwise
  301. */
  302. function setFetchMode($mode) {
  303. $result = false;
  304. switch($mode) {
  305. case PDO_FETCH_NUM:
  306. case PDO_FETCH_ASSOC:
  307. case PDO_FETCH_OBJ:
  308. case PDO_FETCH_BOTH:
  309. $result = true;
  310. $this->__fetchmode = &$mode;
  311. break;
  312. }
  313. return $result;
  314. }
  315. // UNSUPPORTED PUBLIC METHODS
  316. function bindColumn($mixewd, &$param, $type = null, $max_length = null, $driver_option = null) {
  317. return false;
  318. }
  319. function __setErrors($er) {
  320. if(!is_resource($this->__connection)) {
  321. $errno = mysql_errno();
  322. $errst = mysql_error();
  323. }
  324. else {
  325. $errno = mysql_errno($this->__connection);
  326. $errst = mysql_error($this->__connection);
  327. }
  328. $this->__errorCode = &$er;
  329. $this->__errorInfo = Array($this->__errorCode, $errno, $errst);
  330. $this->__result = null;
  331. }
  332. function __uquery(&$query) {
  333. if(!@$query = mysql_query($query, $this->__connection)) {
  334. $this->__setErrors('SQLER');
  335. $query = null;
  336. }
  337. return $query;
  338. }
  339. }
  340. ?>