PDOStatement_mysql.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. if (strexists($v, '\u')) {
  119. $v = str_replace('\u', '\\\\u', $v);
  120. }
  121. if (strexists($v, '\"')) {
  122. $v = str_replace('\"', '\\\\"', $v);
  123. }
  124. array_push($tempr, '"'.mysql_escape_string($v).'"');
  125. }
  126. else {
  127. $parse = create_function('$v', 'return \'"\'.mysql_escape_string($v).\'"\';');
  128. $__query = preg_replace("/(\?)/e", '$parse($array[$k++]);', $__query);
  129. break;
  130. }
  131. }
  132. if(isset($tempf)) {
  133. foreach ($tempf as $k=>$v) {
  134. $search[$k] = '/' . preg_quote($tempf[$k],'`') . '\b/';
  135. }
  136. $__query = preg_replace($search, $tempr, $__query);
  137. //$__query = str_replace($tempf, $tempr, $__query);
  138. }
  139. }
  140. if(is_null($this->__result = &$this->__uquery($__query)))
  141. $keyvars = false;
  142. else
  143. $keyvars = true;
  144. $this->__boundParams = array();
  145. return $keyvars;
  146. }
  147. /**
  148. * Public method:
  149. * Returns, if present, next row of executed query or false.
  150. * this->fetch( $mode:Integer, $cursor:Integer, $offset:Integer ):Mixed
  151. * @Param Integer PDO_FETCH_* constant to know how to read next row, default PDO_FETCH_BOTH
  152. * NOTE: if $mode is omitted is used default setted mode, PDO_FETCH_BOTH
  153. * @Param Integer this variable is not used but respects PDO original accepted parameters
  154. * @Param Integer this variable is not used but respects PDO original accepted parameters
  155. * @Return Mixed Next row of executed query or false if there is nomore.
  156. */
  157. function fetch($mode = PDO_FETCH_ASSOC, $cursor = null, $offset = null) {
  158. if(func_num_args() == 0)
  159. $mode = &$this->__fetchmode;
  160. $result = false;
  161. if(!is_null($this->__result)) {
  162. switch($mode) {
  163. case PDO::FETCH_NUM:
  164. $result = mysql_fetch_row($this->__result);
  165. break;
  166. case PDO::FETCH_ASSOC:
  167. $result = mysql_fetch_assoc($this->__result);
  168. break;
  169. case PDO::FETCH_OBJ:
  170. $result = mysql_fetch_object($this->__result);
  171. break;
  172. case PDO::FETCH_BOTH:
  173. default:
  174. $result = mysql_fetch_array($this->__result);
  175. break;
  176. }
  177. }
  178. if(!$result)
  179. $this->__result = null;
  180. return $result;
  181. }
  182. /**
  183. * Public method:
  184. * Returns an array with all rows of executed query.
  185. * this->fetchAll( $mode:Integer ):Array
  186. * @Param Integer PDO_FETCH_* constant to know how to read all rows, default PDO_FETCH_BOTH
  187. * NOTE: this doesn't work as fetch method, then it will use always PDO_FETCH_BOTH
  188. * if this param is omitted
  189. * @Return Array An array with all fetched rows
  190. */
  191. function fetchAll($mode = PDO_FETCH_ASSOC) {
  192. $result = array();
  193. if(!is_null($this->__result)) {
  194. switch($mode) {
  195. case PDO::FETCH_NUM:
  196. while($r = mysql_fetch_row($this->__result))
  197. array_push($result, $r);
  198. break;
  199. case PDO::FETCH_ASSOC:
  200. while($r = mysql_fetch_assoc($this->__result))
  201. array_push($result, $r);
  202. break;
  203. case PDO::FETCH_OBJ:
  204. while($r = mysql_fetch_object($this->__result))
  205. array_push($result, $r);
  206. break;
  207. case PDO::FETCH_BOTH:
  208. default:
  209. while($r = mysql_fetch_array($this->__result))
  210. array_push($result, $r);
  211. break;
  212. }
  213. }
  214. $this->__result = null;
  215. return $result;
  216. }
  217. /**
  218. * Public method:
  219. * Returns, if present, first column of next row of executed query
  220. * this->fetchSingle( void ):Mixed
  221. * @Return Mixed Null or next row's first column
  222. */
  223. function fetchSingle() {
  224. $result = null;
  225. if(!is_null($this->__result)) {
  226. $result = @mysql_fetch_row($this->__result);
  227. if($result)
  228. $result = $result[0];
  229. else
  230. $this->__result = null;
  231. }
  232. return $result;
  233. }
  234. function fetchColumn($column=0) {
  235. $row = mysql_fetch_row($this->__result);
  236. return $row[$column];
  237. }
  238. /**
  239. * Public method:
  240. * Returns number of last affected database rows
  241. * this->rowCount( void ):Integer
  242. * @Return Integer number of last affected rows
  243. * NOTE: works with INSERT, UPDATE and DELETE query type
  244. */
  245. function rowCount() {
  246. return mysql_affected_rows($this->__connection);
  247. }
  248. // NOT TOTALLY SUPPORTED PUBLIC METHODS
  249. /**
  250. * Public method:
  251. * Quotes correctly a string for this database
  252. * this->getAttribute( $attribute:Integer ):Mixed
  253. * @Param Integer a constant [ PDO_ATTR_SERVER_INFO,
  254. * PDO_ATTR_SERVER_VERSION,
  255. * PDO_ATTR_CLIENT_VERSION,
  256. * PDO_ATTR_PERSISTENT ]
  257. * @Return Mixed correct information or false
  258. */
  259. function getAttribute($attribute) {
  260. $result = false;
  261. switch($attribute) {
  262. case PDO_ATTR_SERVER_INFO:
  263. $result = mysql_get_host_info($this->__connection);
  264. break;
  265. case PDO_ATTR_SERVER_VERSION:
  266. $result = mysql_get_server_info($this->__connection);
  267. break;
  268. case PDO_ATTR_CLIENT_VERSION:
  269. $result = mysql_get_client_info();
  270. break;
  271. case PDO_ATTR_PERSISTENT:
  272. $result = $this->__persistent;
  273. break;
  274. }
  275. return $result;
  276. }
  277. /**
  278. * Public method:
  279. * Sets database attributes, in this version only connection mode.
  280. * this->setAttribute( $attribute:Integer, $mixed:Mixed ):Boolean
  281. * @Param Integer PDO_* constant, in this case only PDO_ATTR_PERSISTENT
  282. * @Param Mixed value for PDO_* constant, in this case a Boolean value
  283. * true for permanent connection, false for default not permament connection
  284. * @Return Boolean true on change, false otherwise
  285. */
  286. function setAttribute($attribute, $mixed) {
  287. $result = false;
  288. if($attribute === PDO_ATTR_PERSISTENT && $mixed != $this->__persistent) {
  289. $result = true;
  290. $this->__persistent = (boolean) $mixed;
  291. mysql_close($this->__connection);
  292. if($this->__persistent === true)
  293. $this->__connection = &mysql_pconnect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  294. else
  295. $this->__connection = &mysql_connect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  296. mysql_select_db($this->__dbinfo[3], $this->__connection);
  297. }
  298. return $result;
  299. }
  300. /**
  301. * Public method:
  302. * Sets default fetch mode to use with this->fetch() method.
  303. * this->setFetchMode( $mode:Integer ):Boolean
  304. * @Param Integer PDO_FETCH_* constant to use while reading an execute query with fetch() method.
  305. * NOTE: PDO_FETCH_LAZY and PDO_FETCH_BOUND are not supported
  306. * @Return Boolean true on change, false otherwise
  307. */
  308. function setFetchMode($mode) {
  309. $result = false;
  310. switch($mode) {
  311. case PDO_FETCH_NUM:
  312. case PDO_FETCH_ASSOC:
  313. case PDO_FETCH_OBJ:
  314. case PDO_FETCH_BOTH:
  315. $result = true;
  316. $this->__fetchmode = &$mode;
  317. break;
  318. }
  319. return $result;
  320. }
  321. // UNSUPPORTED PUBLIC METHODS
  322. function bindColumn($mixewd, &$param, $type = null, $max_length = null, $driver_option = null) {
  323. return false;
  324. }
  325. function __setErrors($er) {
  326. if(!is_resource($this->__connection)) {
  327. $errno = mysql_errno();
  328. $errst = mysql_error();
  329. }
  330. else {
  331. $errno = mysql_errno($this->__connection);
  332. $errst = mysql_error($this->__connection);
  333. }
  334. $this->__errorCode = &$er;
  335. $this->__errorInfo = Array($this->__errorCode, $errno, $errst);
  336. $this->__result = null;
  337. }
  338. function __uquery(&$query) {
  339. if(!@$query = mysql_query($query, $this->__connection)) {
  340. $this->__setErrors('SQLER');
  341. $query = null;
  342. }
  343. return $query;
  344. }
  345. }
  346. ?>