PDO_mysql.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /** File PDO_mysql.class.php *
  3. *(C) Andrea Giammarchi [2005/10/13] */
  4. // Requires PDOStatement_mysql.class.php , drived by PDO.class.php file
  5. require_once('PDOStatement_mysql.class.php');
  6. /**
  7. * Class PDO_mysql
  8. * This class is used from class PDO to manage a MySQL database.
  9. * Look at PDO.clas.php file comments to know more about MySQL connection.
  10. * ---------------------------------------------
  11. * @Compatibility >= PHP 4
  12. * @Dependencies PDO.class.php
  13. * PDOStatement_mysql.class.php
  14. * @Author Andrea Giammarchi
  15. * @Site http://www.devpro.it/
  16. * @Mail andrea [ at ] 3site [ dot ] it
  17. * @Date 2005/10/13
  18. * @LastModified 2005/18/14 12:30
  19. * @Version 0.1 - tested
  20. */
  21. class PDO_mysql {
  22. /**
  23. * 'Private' variables:
  24. * __connection:Resource Database connection
  25. * __dbinfo:Array Array with 4 elements used to manage connection
  26. * __persistent:Boolean Connection mode, is true on persistent, false on normal (deafult) connection
  27. * __errorCode:String Last error code
  28. * __errorInfo:Array Detailed errors
  29. */
  30. var $__connection;
  31. var $__dbinfo;
  32. var $__persistent = false;
  33. var $__errorCode = '';
  34. var $__errorInfo = Array('');
  35. /**
  36. * Public constructor:
  37. * Checks connection and database selection
  38. * new PDO_mysql( &$host:String, &$db:String, &$user:String, &$pass:String )
  39. * @Param String host with or without port info
  40. * @Param String database name
  41. * @Param String database user
  42. * @Param String database password
  43. */
  44. function PDO_mysql(&$host, &$db, &$user, &$pass) {
  45. if(!@$this->__connection = &mysql_connect($host, $user, $pass))
  46. $this->__setErrors('DBCON');
  47. else {
  48. if(!@mysql_select_db($db, $this->__connection))
  49. $this->__setErrors('DBER');
  50. else
  51. $this->__dbinfo = Array($host, $user, $pass, $db);
  52. }
  53. }
  54. /** NOT NATIVE BUT MAYBE USEFULL FOR PHP < 5.1 PDO DRIVER
  55. * Public method
  56. * Calls mysql_close function.
  57. * this->close( Void ):Boolean
  58. * @Return Boolean True on success, false otherwise
  59. */
  60. function close() {
  61. $result = is_resource($this->__connection);
  62. if($result) {
  63. mysql_close($this->__connection);
  64. }
  65. return $result;
  66. }
  67. /**
  68. * Public method:
  69. * Returns a code rappresentation of an error
  70. * this->errorCode( void ):String
  71. * @Return String String rappresentation of the error
  72. */
  73. function errorCode() {
  74. return $this->__errorCode;
  75. }
  76. /**
  77. * Public method:
  78. * Returns an array with error informations
  79. * this->errorInfo( void ):Array
  80. * @Return Array Array with 3 keys:
  81. * 0 => error code
  82. * 1 => error number
  83. * 2 => error string
  84. */
  85. function errorInfo() {
  86. return $this->__errorInfo;
  87. }
  88. /**
  89. * Public method:
  90. * Excecutes a query and returns affected rows
  91. * this->exec( $query:String ):Mixed
  92. * @Param String query to execute
  93. * @Return Mixed Number of affected rows or false on bad query.
  94. */
  95. function exec($query) {
  96. $result = 0;
  97. if(!is_null($this->__uquery($query)))
  98. $result = mysql_affected_rows($this->__connection);
  99. if(is_null($result))
  100. $result = false;
  101. return $result;
  102. }
  103. /**
  104. * Public method:
  105. * Returns last inserted id
  106. * this->lastInsertId( void ):Number
  107. * @Return Number Last inserted id
  108. */
  109. function lastInsertId() {
  110. $id = mysql_insert_id($this->__connection);
  111. if ($id > 0) {
  112. return $id;
  113. } else {
  114. $query = $this->prepare('SELECT last_insert_id()');
  115. $query->execute();
  116. return $query->fetchColumn();
  117. }
  118. }
  119. /**
  120. * Public method:
  121. * Returns a new PDOStatement
  122. * this->prepare( $query:String, $array:Array ):PDOStatement
  123. * @Param String query to prepare
  124. * @Param Array this variable is not used but respects PDO original accepted parameters
  125. * @Return PDOStatement new PDOStatement to manage
  126. */
  127. function prepare($query, $array = Array()) {
  128. return new PDOStatement_mysql($query, $this->__connection, $this->__dbinfo);
  129. }
  130. /**
  131. * Public method:
  132. * Executes directly a query and returns an array with result or false on bad query
  133. * this->query( $query:String ):Mixed
  134. * @Param String query to execute
  135. * @Return Mixed false on error, array with all info on success
  136. */
  137. function query($query) {
  138. $query = @mysql_unbuffered_query($query, $this->__connection);
  139. if($query) {
  140. $result = Array();
  141. while($r = mysql_fetch_assoc($query))
  142. array_push($result, $r);
  143. }
  144. else {
  145. $result = false;
  146. $this->__setErrors('SQLER');
  147. }
  148. return $result;
  149. }
  150. /**
  151. * Public method:
  152. * Quotes correctly a string for this database
  153. * this->quote( $string:String ):String
  154. * @Param String string to quote
  155. * @Return String a correctly quoted string
  156. */
  157. function quote($string) {
  158. return ('"'.mysql_escape_string($string).'"');
  159. }
  160. // NOT TOTALLY SUPPORTED PUBLIC METHODS
  161. /**
  162. * Public method:
  163. * Quotes correctly a string for this database
  164. * this->getAttribute( $attribute:Integer ):Mixed
  165. * @Param Integer a constant [ PDO_ATTR_SERVER_INFO,
  166. * PDO_ATTR_SERVER_VERSION,
  167. * PDO_ATTR_CLIENT_VERSION,
  168. * PDO_ATTR_PERSISTENT ]
  169. * @Return Mixed correct information or false
  170. */
  171. function getAttribute($attribute) {
  172. $result = false;
  173. switch($attribute) {
  174. case PDO_ATTR_SERVER_INFO:
  175. $result = mysql_get_host_info($this->__connection);
  176. break;
  177. case PDO_ATTR_SERVER_VERSION:
  178. $result = mysql_get_server_info($this->__connection);
  179. break;
  180. case PDO_ATTR_CLIENT_VERSION:
  181. $result = mysql_get_client_info();
  182. break;
  183. case PDO_ATTR_PERSISTENT:
  184. $result = $this->__persistent;
  185. break;
  186. }
  187. return $result;
  188. }
  189. /**
  190. * Public method:
  191. * Sets database attributes, in this version only connection mode.
  192. * this->setAttribute( $attribute:Integer, $mixed:Mixed ):Boolean
  193. * @Param Integer PDO_* constant, in this case only PDO_ATTR_PERSISTENT
  194. * @Param Mixed value for PDO_* constant, in this case a Boolean value
  195. * true for permanent connection, false for default not permament connection
  196. * @Return Boolean true on change, false otherwise
  197. */
  198. function setAttribute($attribute, $mixed) {
  199. $result = false;
  200. if($attribute === PDO_ATTR_PERSISTENT && $mixed != $this->__persistent) {
  201. $result = true;
  202. $this->__persistent = (boolean) $mixed;
  203. mysql_close($this->__connection);
  204. if($this->__persistent === true)
  205. $this->__connection = &mysql_pconnect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  206. else
  207. $this->__connection = &mysql_connect($this->__dbinfo[0], $this->__dbinfo[1], $this->__dbinfo[2]);
  208. mysql_select_db($this->__dbinfo[3], $this->__connection);
  209. }
  210. return $result;
  211. }
  212. // UNSUPPORTED PUBLIC METHODS
  213. function beginTransaction() {
  214. return false;
  215. }
  216. function commit() {
  217. return false;
  218. }
  219. function rollBack() {
  220. return false;
  221. }
  222. // PRIVATE METHODS [ UNCOMMENTED ]
  223. function __setErrors($er) {
  224. if(!is_resource($this->__connection)) {
  225. $errno = mysql_errno();
  226. $errst = mysql_error();
  227. }
  228. else {
  229. $errno = mysql_errno($this->__connection);
  230. $errst = mysql_error($this->__connection);
  231. }
  232. $this->__errorCode = &$er;
  233. $this->__errorInfo = Array($this->__errorCode, $errno, $errst);
  234. }
  235. function __uquery(&$query) {
  236. if(!@$query = mysql_query($query, $this->__connection)) {
  237. $this->__setErrors('SQLER');
  238. $query = null;
  239. }
  240. return $query;
  241. }
  242. }
  243. ?>