Result.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * MySQL database result. See [Results](/database/results) for usage and examples.
  4. *
  5. * @package Kohana/Database
  6. * @category Query/Result
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. class Kohana_Database_MySQL_Result extends Database_Result {
  12. protected $_internal_row = 0;
  13. public function __construct($result, $sql, $as_object = FALSE, array $params = NULL)
  14. {
  15. parent::__construct($result, $sql, $as_object, $params);
  16. // Find the number of rows in the result
  17. $this->_total_rows = mysql_num_rows($result);
  18. }
  19. public function __destruct()
  20. {
  21. if (is_resource($this->_result))
  22. {
  23. mysql_free_result($this->_result);
  24. }
  25. }
  26. public function seek($offset)
  27. {
  28. if ($this->offsetExists($offset) AND mysql_data_seek($this->_result, $offset))
  29. {
  30. // Set the current row to the offset
  31. $this->_current_row = $this->_internal_row = $offset;
  32. return TRUE;
  33. }
  34. else
  35. {
  36. return FALSE;
  37. }
  38. }
  39. public function current()
  40. {
  41. if ($this->_current_row !== $this->_internal_row AND ! $this->seek($this->_current_row))
  42. return NULL;
  43. // Increment internal row for optimization assuming rows are fetched in order
  44. $this->_internal_row++;
  45. if ($this->_as_object === TRUE)
  46. {
  47. // Return an stdClass
  48. return mysql_fetch_object($this->_result);
  49. }
  50. elseif (is_string($this->_as_object))
  51. {
  52. // Return an object of given class name
  53. return mysql_fetch_object($this->_result, $this->_as_object, $this->_object_params);
  54. }
  55. else
  56. {
  57. // Return an array of the row
  58. return mysql_fetch_assoc($this->_result);
  59. }
  60. }
  61. } // End Database_MySQL_Result_Select