MysqlResult.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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) 2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * This class will convert mysql result resource to array suitable for passing
  23. * to the external entities.
  24. *
  25. * @package Zend_Amf
  26. * @subpackage Parse
  27. * @copyright Copyright (c) 2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Amf_Parse_Resource_MysqlResult
  31. {
  32. /**
  33. * @var array List of Mysql types with PHP counterparts
  34. *
  35. * Key => Value is Mysql type (exact string) => PHP type
  36. */
  37. static public $fieldTypes = array(
  38. "int" => "int",
  39. "timestamp" => "int",
  40. "year" => "int",
  41. "real" => "float",
  42. );
  43. /**
  44. * Parse resource into array
  45. *
  46. * @param resource $resource
  47. * @return array
  48. */
  49. public function parse($resource) {
  50. $result = array();
  51. $fieldcnt = mysql_num_fields($resource);
  52. $fields_transform = array();
  53. for($i=0;$i<$fieldcnt;$i++) {
  54. $type = mysql_field_type($resource, $i);
  55. if(isset(self::$fieldTypes[$type])) {
  56. $fields_transform[mysql_field_name($resource, $i)] = self::$fieldTypes[$type];
  57. }
  58. }
  59. while($row = mysql_fetch_assoc($resource)) {
  60. foreach($fields_transform as $fieldname => $fieldtype) {
  61. settype($row[$fieldname], $fieldtype);
  62. }
  63. $result[] = $row;
  64. }
  65. return $result;
  66. }
  67. }