/library/PDO_functions.php

https://gitlab.com/tutaalexandr/bot_local · PHP · 138 lines · 87 code · 7 blank · 44 comment · 4 complexity · d933df03b59c9560e2f7e92fb4b7d02e MD5 · raw file

  1. <?php
  2. /***************************************
  3. * http://www.program-o.com
  4. * PROGRAM O
  5. * Version: 2.4.8
  6. * FILE: library/PDO_functions.php
  7. * AUTHOR: Elizabeth Perreau and Dave Morton
  8. * DATE: MAY 17TH 2014
  9. * DETAILS: common library of db functions
  10. ***************************************/
  11. /**
  12. * function db_open()
  13. * Connect to the database
  14. *
  15. * @link http://blog.program-o.com/?p=1340
  16. * @internal param string $host - db host
  17. * @internal param string $user - db user
  18. * @internal param string $password - db password
  19. * @internal param string $database_name - db name
  20. * @return resource $dbConn - the database connection resource
  21. */
  22. function db_open()
  23. {
  24. global $dbh, $dbu, $dbp, $dbn, $dbPort;
  25. try {
  26. $dbConn = new PDO("mysql:host=$dbh;dbname=$dbn;charset=utf8", $dbu, $dbp);
  27. $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  28. $dbConn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  29. $dbConn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  30. }
  31. catch (Exception $e)
  32. {
  33. exit('Program O has encountered a problem with connecting to the database. With any luck, the following message will help: ' . $e->getMessage());
  34. }
  35. return $dbConn;
  36. }
  37. /**
  38. * function db_close()
  39. * Close the connection to the database
  40. *
  41. * @link http://blog.program-o.com/?p=1343
  42. * @internal param resource $dbConn - the open connection
  43. *
  44. * @return null
  45. */
  46. function db_close()
  47. {
  48. runDebug(__FILE__, __FUNCTION__, __LINE__, 'This DB is now closed. You don\'t have to go home, but you can\'t stay here.', 2);
  49. return null;
  50. }
  51. /*
  52. * function db_fetch
  53. * Fetches a single row of data from the database
  54. *
  55. * @param (string) $sql - The SQL query to execute
  56. * @param (mixed) $params - either an array of placeholder/value pairs, or null, for no parameters
  57. * @param (string) $file - the path/filename of the file that the function call originated in
  58. * @param (string) $function - the name of the function that the function call originated in
  59. * @param (string) $line - the line number of the originating function call
  60. *
  61. * @return (mixed) $out - Either the row of data from the DB query, or false, if the query fails
  62. */
  63. function db_fetch($sql, $params = null, $file = 'unknown', $function = 'unknown', $line = 'unknown')
  64. {
  65. global $dbConn;
  66. //error_log(print_r($dbConn, true), 3, _LOG_PATH_ . 'dbConn.txt');
  67. try
  68. {
  69. $sth = $dbConn->prepare($sql);
  70. ($params === null) ? $sth->execute() : $sth->execute($params);
  71. $out = $sth->fetch();
  72. return $out;
  73. }
  74. catch (Exception $e)
  75. {
  76. //error_log("bad SQL encountered in file $file, line #$line. SQL:\n$sql\n", 3, _LOG_PATH_ . 'badSQL.txt');
  77. $pdoError = print_r($dbConn->errorInfo(), true);
  78. $psError = print_r($sth->errorInfo(), true);
  79. runDebug(__FILE__, __FUNCTION__, __LINE__, "An error was generated while extracting a row of data from the database in file $file at line $line, in the function $function - SQL:\n$sql\nPDO error: $pdoError\nPDOStatement error: $psError", 0);
  80. return false;
  81. }
  82. }
  83. function db_fetchAll($sql, $params = null, $file = 'unknown', $function = 'unknown', $line = 'unknown')
  84. {
  85. global $dbConn;
  86. try
  87. {
  88. $sth = $dbConn->prepare($sql);
  89. ($params === null) ? $sth->execute() : $sth->execute($params);
  90. return $sth->fetchAll();
  91. }
  92. catch (Exception $e)
  93. {
  94. //error_log("bad SQL encountered in file $file, line #$line. SQL:\n$sql\n", 3, _LOG_PATH_ . 'badSQL.txt');
  95. $pdoError = print_r($dbConn->errorInfo(), true);
  96. $psError = print_r($sth->errorInfo(), true);
  97. runDebug(__FILE__, __FUNCTION__, __LINE__, "An error was generated while extracting multiple rows of data from the database in file $file at line $line, in the function $function - SQL:\n$sql\nPDO error: $pdoError\nPDOStatement error: $psError", 0);
  98. return false;
  99. }
  100. }
  101. function db_write($sql, $params = null, $multi = false, $file = 'unknown', $function = 'unknown', $line = 'unknown')
  102. {
  103. global $dbConn;
  104. try
  105. {
  106. $sth = $dbConn->prepare($sql);
  107. switch (true)
  108. {
  109. case ($params === null):
  110. $sth->execute();
  111. break;
  112. case ($multi === true):
  113. foreach ($params as $row)
  114. {
  115. $sth->execute($row);
  116. }
  117. break;
  118. default:
  119. $sth->execute($params);
  120. }
  121. return $sth->rowCount();
  122. }
  123. catch (Exception $e)
  124. {
  125. $pdoError = print_r($dbConn->errorInfo(), true);
  126. $psError = print_r($sth->errorInfo(), true);
  127. error_log("bad SQL encountered in file $file, line #$line. SQL:\n$sql\nPDO Error:\n$pdoError\nSTH Error:\n$psError\nException Message:\n" . $e->getMessage() . "\n", 3, _LOG_PATH_ . 'db_write.txt');
  128. runDebug(__FILE__, __FUNCTION__, __LINE__, "An error was generated while writing to the database in file $file at line $line, in the function $function - SQL:\n$sql\nPDO error: $pdoError\nPDOStatement error: $psError", 0);
  129. return false;
  130. }
  131. }