MysqlResult.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Amf
  17. * @subpackage Parse
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: MysqlResult.php 2504 2011-12-28 07:35:29Z liu21st $
  21. */
  22. /**
  23. * This class will convert mysql result resource to array suitable for passing
  24. * to the external entities.
  25. *
  26. * @package Zend_Amf
  27. * @subpackage Parse
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Amf_Parse_Resource_MysqlResult
  32. {
  33. /**
  34. * @var array List of Mysql types with PHP counterparts
  35. *
  36. * Key => Value is Mysql type (exact string) => PHP type
  37. */
  38. static public $fieldTypes = array(
  39. "int" => "int",
  40. "timestamp" => "int",
  41. "year" => "int",
  42. "real" => "float",
  43. );
  44. /**
  45. * Parse resource into array
  46. *
  47. * @param resource $resource
  48. * @return array
  49. */
  50. public function parse($resource) {
  51. $result = array();
  52. $fieldcnt = mysql_num_fields($resource);
  53. $fields_transform = array();
  54. for($i=0;$i<$fieldcnt;$i++) {
  55. $type = mysql_field_type($resource, $i);
  56. if(isset(self::$fieldTypes[$type])) {
  57. $fields_transform[mysql_field_name($resource, $i)] = self::$fieldTypes[$type];
  58. }
  59. }
  60. while($row = mysql_fetch_object($resource)) {
  61. foreach($fields_transform as $fieldname => $fieldtype) {
  62. settype($row->$fieldname, $fieldtype);
  63. }
  64. $result[] = $row;
  65. }
  66. return $result;
  67. }
  68. }