result.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\MySQL;
  3. use MGModule\DNSManager2\mgLibs\custom\helpers as helpers;
  4. /**
  5. * MySQL Results Class
  6. *
  7. * @author Michal Czech <michael@modulesgarden.com>
  8. */
  9. class result
  10. {
  11. /**
  12. *
  13. * @var PDOStatement
  14. */
  15. private $result;
  16. /**
  17. * Use PDO for Connection
  18. *
  19. * @var boolean
  20. */
  21. static private $usePDO = false;
  22. /**
  23. * Constructor
  24. *
  25. * @author Michal Czech <michael@modulesgarden.com>
  26. * @param PDOStatement $result
  27. * @param int $id
  28. */
  29. function __construct($result,$id = null) {
  30. if(is_a($result,'PDOStatement'))
  31. {
  32. self::$usePDO = true;
  33. }
  34. $this->result = $result;
  35. $this->id = $id;
  36. }
  37. /**
  38. * Fetch one record
  39. *
  40. * @author Michal Czech <michael@modulesgarden.com>
  41. * @return array
  42. */
  43. function fetch()
  44. {
  45. return $this->result->fetch(\PDO::FETCH_ASSOC);
  46. }
  47. /**
  48. * Fetch All Records
  49. *
  50. * @author Michal Czech <michael@modulesgarden.com>
  51. * @return array
  52. */
  53. function fetchAll()
  54. {
  55. if(self::$usePDO)
  56. {
  57. return $this->result->fetchAll(\PDO::FETCH_ASSOC);
  58. }
  59. else
  60. {
  61. $result = array();
  62. while($row = $this->fetch())
  63. {
  64. $result[] = $row;
  65. }
  66. return $result;
  67. }
  68. }
  69. function fetchAllKeyPair()
  70. {
  71. return $this->result->fetchAll(\PDO::FETCH_KEY_PAIR);
  72. }
  73. function fetchGroup()
  74. {
  75. return $this->result->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_UNIQUE|\PDO::FETCH_ASSOC);
  76. }
  77. /**
  78. * Fetch One Column From First Record
  79. *
  80. * @author Michal Czech <michael@modulesgarden.com>
  81. * @param string $name
  82. * @return array
  83. */
  84. function fetchColumn($name = null)
  85. {
  86. $data = $this->result->fetch(\PDO::FETCH_BOTH);
  87. if($name)
  88. {
  89. return $data[$name];
  90. }
  91. else
  92. {
  93. return $data[0];
  94. }
  95. }
  96. /**
  97. * Get ID Last Inserted Record
  98. *
  99. * @author Michal Czech <michael@modulesgarden.com>
  100. * @return int
  101. */
  102. function getID()
  103. {
  104. return $this->id;
  105. }
  106. function numRows()
  107. {
  108. $this->result->fetch(\PDO::FETCH_BOTH);
  109. return $this->result->rowCount();
  110. }
  111. }