MysqliResult.php 4.0 KB

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