Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * PDO abstraction
  5.  *
  6.  */
  7.  
  8. global $UNC_DB;
  9. $default_config = array(
  10.     'database' => 'default',
  11.     'username' => 'default',
  12.     'server' => 'localhost',
  13.     'password' => 'default',
  14. );
  15.  
  16. // let's set the
  17. foreach ($default_config as $type => $value) {
  18.     if (!isset($UNC_DB[$type])) {
  19.         $UNC_DB[$type] = $value;
  20.     }
  21. }
  22.  
  23. if (!function_exists('XMPP_ERROR_trace')) {
  24.     die('ERROR checking failed, install XMPP_ERROR!');
  25. }
  26.  
  27. // new way
  28. $UNC_DB['link'] = new PDO("mysql:host={$UNC_DB['server']};dbname={$UNC_DB['database']}", $UNC_DB['username'], $UNC_DB['password']);
  29. $UNC_DB['link']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  30.  
  31. /**
  32.  * Replacement for mysql_query
  33.  * returns result. includes error notification
  34.  *
  35.  * can close the query if needed, otherwise returns result
  36.  *
  37.  * @global PDO $UMC_DB
  38.  * @param type $sql
  39.  * @param type $close
  40.  * @return type
  41.  */
  42. function umc_mysql_query($sql, $close = false) {
  43.     global $UNC_DB;
  44.     XMPP_ERROR_trace(__FUNCTION__, func_get_args());
  45.     $rst = $UNC_DB['link']->query($sql);
  46.     $error = $UNC_DB['link']->errorInfo();
  47.     if (!is_null($error[2])) {
  48.         XMPP_ERROR_trigger("MySQL Query Error: '$sql' : " . $error[2]);
  49.     } else if ($close) {
  50.         $rst->closeCursor();
  51.     } else {
  52.         return $rst;
  53.     }
  54. }
  55.  
  56. /**
  57.  * Replacement for mysql_insert_id
  58.  *
  59.  * @global PDO $UMC_DB
  60.  * @return type integer
  61.  */
  62. function umc_mysql_insert_id() {
  63.     global $UNC_DB;
  64.     return $UNC_DB['link']->lastInsertId();
  65. }
  66.  
  67. /**
  68.  * Replacement for mysql_fetch_array (MYSQL_ASSOC)
  69.  * Returns one line of associative arrays
  70.  *
  71.  * @param type $rst
  72.  * @return type
  73.  */
  74. function umc_mysql_fetch_array($rst) {
  75.     XMPP_ERROR_trace(__FUNCTION__, func_get_args());
  76.     if (!$rst) {
  77.         XMPP_ERROR_trigger("tried fetch_array on erroneous recordset");
  78.         return false;
  79.     }
  80.     $row = $rst->fetch(PDO::FETCH_ASSOC);
  81.     return $row;
  82. }
  83.  
  84. /**
  85.  * Replacement of mysql_free_result
  86.  *
  87.  * @param type $rst
  88.  */
  89. function umc_mysql_free_result($rst) {
  90.     $rst->closeCursor();
  91. }
  92.  
  93. /**
  94.  * Replacement of mysql_real_escape_string
  95.  * ATTENTION: This also puts quotes around the value
  96.  *
  97.  * @global PDO $UMC_DB
  98.  * @param type $value
  99.  * @return type
  100.  */
  101. function umc_mysql_real_escape_string($value) {
  102.     global $UNC_DB;
  103.     return $UNC_DB['link']->quote($value);
  104. }
  105.  
  106. function umc_mysql_fetch_all($sql) {
  107.     global $UNC_DB;
  108.     XMPP_ERROR_trace(__FUNCTION__, func_get_args());
  109.     $stmt = $UNC_DB['link']->prepare($sql);
  110.     if (!$stmt) {
  111.         $error = $UNC_DB['link']->errorInfo();
  112.         XMPP_ERROR_trigger($error);
  113.         return false;
  114.     } else {
  115.         $stmt->execute();
  116.     }
  117.     $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  118.     return $result;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement