An PHP based Image Database
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1 KiB

  1. <?php
  2. //
  3. // Copyright (c) 2013 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
  4. //
  5. // Permission to use, copy, modify, and/or distribute this software for any purpose
  6. // with or without fee is hereby granted, provided that the above copyright notice
  7. // and this permission notice appear in all copies.
  8. //
  9. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  10. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  11. // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  12. // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. // ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. //
  16. function connectToDatabase() {
  17. global $_MGM;
  18. if (isset($_MGM['DBConnection'])) closeDatabase();
  19. $_MGM['DBConnection'] = NULL;
  20. $_MGM['DBConnection'] = new PDO("sqlite:".$_MGM['DBName']);
  21. $_MGM['DBConnection']->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  22. if ($_MGM['DBPersistent'])
  23. $_MGM['DBConnection']->setAttribute(PDO::ATTR_PERSISTENT, TRUE);
  24. if ($_MGM['DBConnection']==NULL) error("Database Connection Failed");
  25. }
  26. function closeDatabase() {
  27. global $_MGM;
  28. if (isset($_MGM['DBConnection'])) {
  29. $_MGM['DBConnection'] = NULL;
  30. }
  31. }
  32. function escapeString($theString) {
  33. global $_MGM;
  34. return $_MGM['DBConnection']->quote($theString);
  35. }
  36. function quoteObject($theObject) {
  37. global $_MGM;
  38. if (is_null($theObject)) {
  39. return "NULL";
  40. } else if (is_string($theObject)) {
  41. return escapeString($theObject);
  42. } else if (is_float($theObject) || is_integer($theObject)) {
  43. return $theObject;
  44. } else if (is_bool($theObject)) {
  45. return ($theObject ? 1 : 0);
  46. }
  47. return "NULL";
  48. }
  49. function databaseQuery($format) {
  50. global $_MGM;
  51. $result = NULL;
  52. try {
  53. if (isset($_MGM['DBConnection'])) {
  54. $args = func_get_args();
  55. array_shift($args);
  56. $args = array_map("quoteObject", $args);
  57. $query = vsprintf($format, $args);
  58. $result = $_MGM['DBConnection']->query($query);
  59. }
  60. //if ($result==NULL) error("Failed to run query on database");
  61. } catch (Exception $e) {
  62. //echo $e->getMessage()."<br />\n";
  63. //error("Failed to run query on database");
  64. }
  65. return $result;
  66. }
  67. function databaseRowCount($theResult) {
  68. global $_MGM;
  69. if ($theResult==NULL)
  70. return 0;
  71. return $theResult->rowCount();
  72. }
  73. function databaseFieldCount($theResult) {
  74. global $_MGM;
  75. if ($theResult==NULL)
  76. return 0;
  77. return $theResult->columnCount();
  78. }
  79. function databaseLastID() {
  80. global $_MGM;
  81. $result = 0;
  82. if (isset($_MGM['DBConnection'])) {
  83. $result = $_MGM['DBConnection']->lastInsertId();
  84. }
  85. return $result;
  86. }
  87. function databaseFetch($theResult) {
  88. global $_MGM;
  89. return $theResult->fetch();
  90. }
  91. function databaseFetchNum($theResult) {
  92. global $_MGM;
  93. return $theResult->fetch(PDO::FETCH_NUM);
  94. }
  95. function databaseFetchAssoc($theResult) {
  96. global $_MGM;
  97. return $theResult->fetch(PDO::FETCH_ASSOC);
  98. }
  99. function databaseResultSeek($theResult, $theLocation) {
  100. global $_MGM;
  101. return false;
  102. }
  103. function databaseFreeResult($theResult) {
  104. global $_MGM;
  105. $theResult = NULL;
  106. }
  107. ?>