From fc7564545fb166553a7080b8dbd2b9941925734d Mon Sep 17 00:00:00 2001 From: danmontgomery Date: Sun, 21 Aug 2011 15:31:22 -0400 Subject: Fixed issue #150 (for mysql and mysqli), now returns the actual column length. --- system/database/drivers/mysql/mysql_driver.php | 2 +- system/database/drivers/mysql/mysql_result.php | 17 +++++++++++------ system/database/drivers/mysqli/mysqli_driver.php | 2 +- system/database/drivers/mysqli/mysqli_result.php | 19 ++++++++++++------- user_guide/changelog.html | 1 + 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 872504564..f87cfea4b 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -441,7 +441,7 @@ class CI_DB_mysql_driver extends CI_DB { */ function _field_data($table) { - return "SELECT * FROM ".$table." LIMIT 1"; + return "DESCRIBE ".$table; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysql/mysql_result.php b/system/database/drivers/mysql/mysql_result.php index 507389603..2d2905c98 100644 --- a/system/database/drivers/mysql/mysql_result.php +++ b/system/database/drivers/mysql/mysql_result.php @@ -84,14 +84,19 @@ class CI_DB_mysql_result extends CI_DB_result { function field_data() { $retval = array(); - while ($field = mysql_fetch_field($this->result_id)) + while ($field = mysql_fetch_object($this->result_id)) { + preg_match('/([a-zA-Z]+)\((\d+)\)/', $field->Type, $matches); + + $type = $matches[1]; + $length = (int)$matches[2]; + $F = new stdClass(); - $F->name = $field->name; - $F->type = $field->type; - $F->default = $field->def; - $F->max_length = $field->max_length; - $F->primary_key = $field->primary_key; + $F->name = $field->Field; + $F->type = $type; + $F->default = $field->Default; + $F->max_length = $length; + $F->primary_key = ( $field->Key == 'PRI' ? 1 : 0 ); $retval[] = $F; } diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index ddcaff323..ccd110f79 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -442,7 +442,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ function _field_data($table) { - return "SELECT * FROM ".$table." LIMIT 1"; + return "DESCRIBE ".$table; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php index c4d8f5d58..ac863056a 100644 --- a/system/database/drivers/mysqli/mysqli_result.php +++ b/system/database/drivers/mysqli/mysqli_result.php @@ -84,21 +84,26 @@ class CI_DB_mysqli_result extends CI_DB_result { function field_data() { $retval = array(); - while ($field = mysqli_fetch_field($this->result_id)) + while ($field = mysqli_fetch_object($this->result_id)) { + preg_match('/([a-zA-Z]+)\((\d+)\)/', $field->Type, $matches); + + $type = $matches[1]; + $length = (int)$matches[2]; + $F = new stdClass(); - $F->name = $field->name; - $F->type = $field->type; - $F->default = $field->def; - $F->max_length = $field->max_length; - $F->primary_key = ($field->flags & MYSQLI_PRI_KEY_FLAG) ? 1 : 0; + $F->name = $field->Field; + $F->type = $type; + $F->default = $field->Default; + $F->max_length = $length; + $F->primary_key = ( $field->Key == 'PRI' ? 1 : 0 ); $retval[] = $F; } return $retval; } - + // -------------------------------------------------------------------- /** diff --git a/user_guide/changelog.html b/user_guide/changelog.html index 15872c1ac..5782ac0fb 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -99,6 +99,7 @@ Change Log
  • Fixed a bug (#200) where MySQL queries would be malformed after calling count_all() then db->get()
  • Fixed a bug (#181) where a mis-spelling was in the form validation language file.
  • Fixed a bug (#160) - Removed unneeded array copy in the file cache driver.
  • +
  • Fixed a bug (#150) - field_data() now correctly returns column length.
  • Version 2.0.3

    -- cgit v1.2.3-24-g4f1b