MysqliResult.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: MysqliResult.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_MysqliResult
  32. {
  33. /**
  34. * mapping taken from http://forums.mysql.com/read.php?52,255868,255895#msg-255895
  35. */
  36. static public $mysqli_type = array(
  37. 0 => "MYSQLI_TYPE_DECIMAL",
  38. 1 => "MYSQLI_TYPE_TINYINT",
  39. 2 => "MYSQLI_TYPE_SMALLINT",
  40. 3 => "MYSQLI_TYPE_INTEGER",
  41. 4 => "MYSQLI_TYPE_FLOAT",
  42. 5 => "MYSQLI_TYPE_DOUBLE",
  43. 7 => "MYSQLI_TYPE_TIMESTAMP",
  44. 8 => "MYSQLI_TYPE_BIGINT",
  45. 9 => "MYSQLI_TYPE_MEDIUMINT",
  46. 10 => "MYSQLI_TYPE_DATE",
  47. 11 => "MYSQLI_TYPE_TIME",
  48. 12 => "MYSQLI_TYPE_DATETIME",
  49. 13 => "MYSQLI_TYPE_YEAR",
  50. 14 => "MYSQLI_TYPE_DATE",
  51. 16 => "MYSQLI_TYPE_BIT",
  52. 246 => "MYSQLI_TYPE_DECIMAL",
  53. 247 => "MYSQLI_TYPE_ENUM",
  54. 248 => "MYSQLI_TYPE_SET",
  55. 249 => "MYSQLI_TYPE_TINYBLOB",
  56. 250 => "MYSQLI_TYPE_MEDIUMBLOB",
  57. 251 => "MYSQLI_TYPE_LONGBLOB",
  58. 252 => "MYSQLI_TYPE_BLOB",
  59. 253 => "MYSQLI_TYPE_VARCHAR",
  60. 254 => "MYSQLI_TYPE_CHAR",
  61. 255 => "MYSQLI_TYPE_GEOMETRY",
  62. );
  63. // Build an associative array for a type look up
  64. static $mysqli_to_php = array(
  65. "MYSQLI_TYPE_DECIMAL" => 'float',
  66. "MYSQLI_TYPE_NEWDECIMAL" => 'float',
  67. "MYSQLI_TYPE_BIT" => 'integer',
  68. "MYSQLI_TYPE_TINYINT" => 'integer',
  69. "MYSQLI_TYPE_SMALLINT" => 'integer',
  70. "MYSQLI_TYPE_MEDIUMINT" => 'integer',
  71. "MYSQLI_TYPE_BIGINT" => 'integer',
  72. "MYSQLI_TYPE_INTEGER" => 'integer',
  73. "MYSQLI_TYPE_FLOAT" => 'float',
  74. "MYSQLI_TYPE_DOUBLE" => 'float',
  75. "MYSQLI_TYPE_NULL" => 'null',
  76. "MYSQLI_TYPE_TIMESTAMP" => 'string',
  77. "MYSQLI_TYPE_INT24" => 'integer',
  78. "MYSQLI_TYPE_DATE" => 'string',
  79. "MYSQLI_TYPE_TIME" => 'string',
  80. "MYSQLI_TYPE_DATETIME" => 'string',
  81. "MYSQLI_TYPE_YEAR" => 'string',
  82. "MYSQLI_TYPE_NEWDATE" => 'string',
  83. "MYSQLI_TYPE_ENUM" => 'string',
  84. "MYSQLI_TYPE_SET" => 'string',
  85. "MYSQLI_TYPE_TINYBLOB" => 'object',
  86. "MYSQLI_TYPE_MEDIUMBLOB" => 'object',
  87. "MYSQLI_TYPE_LONGBLOB" => 'object',
  88. "MYSQLI_TYPE_BLOB" => 'object',
  89. "MYSQLI_TYPE_CHAR" => 'string',
  90. "MYSQLI_TYPE_VARCHAR" => 'string',
  91. "MYSQLI_TYPE_GEOMETRY" => 'object',
  92. "MYSQLI_TYPE_BIT" => 'integer',
  93. );
  94. /**
  95. * Parse resource into array
  96. *
  97. * @param resource $resource
  98. * @return array
  99. */
  100. public function parse($resource) {
  101. $result = array();
  102. $fieldcnt = mysqli_num_fields($resource);
  103. $fields_transform = array();
  104. for($i=0;$i<$fieldcnt;$i++) {
  105. $finfo = mysqli_fetch_field_direct($resource, $i);
  106. if(isset(self::$mysqli_type[$finfo->type])) {
  107. $fields_transform[$finfo->name] = self::$mysqli_to_php[self::$mysqli_type[$finfo->type]];
  108. }
  109. }
  110. while($row = mysqli_fetch_assoc($resource)) {
  111. foreach($fields_transform as $fieldname => $fieldtype) {
  112. settype($row[$fieldname], $fieldtype);
  113. }
  114. $result[] = $row;
  115. }
  116. return $result;
  117. }
  118. }