Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Is there a way to recognize enum/set data type?
Old 08-12-2009, 09:51 PM Is there a way to recognize enum/set data type?
Average Talker

Posts: 17
Trades: 0
I need to write a PHP function that will look at the properties of each field in a database table. I was thinking of using mysql_fetch_field() to this end, like, for example,
$property = mysql_fetch_field($result);
echo $property->type;

But then I noticed that when a field has ENUM or SET datatype, this code still says the data type is "string". So is there any way to make the program recognize ENUM or SET fields, and return their correct datatype and the set of allowed values?
DownWithBugs is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-12-2009, 10:35 PM Re: Is there a way to recognize enum/set data type?
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Technically both of those datatypes are string types ( http://dev.mysql.com/doc/refman/5.0/en/enum.html and http://dev.mysql.com/tech-resources/...-datatype.html ).

As far as the enumerations go, PHP does not have native support for this type. The database, however, will enforce the constraint, but once you pull the value from the database it will be a string. Regarding the set type, find out what the default delimiter is (I think it is , but I've never used this type myself) when you pull if from the database and you should be able to use explode to parse it into an array.

On a side note, neither of those datatypes are used very commonly.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-12-2009, 11:22 PM Re: Is there a way to recognize enum/set data type?
Average Talker

Posts: 17
Trades: 0
Thanks, NullPointer, but how do I find out if the field type is enum?
DownWithBugs is offline
Reply With Quote
View Public Profile
 
Old 08-12-2009, 11:54 PM Re: Is there a way to recognize enum/set data type?
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
Try the Describe command: http://dev.mysql.com/doc/refman/5.0/en/describe.html

Edit: I found a function using SHOW FIELDS that might help:
http://us2.php.net/manual/en/functio...type.php#69718
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 08-12-2009 at 11:58 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-13-2009, 03:46 AM Re: Is there a way to recognize enum/set data type?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I use a modification of the script below to index a database for automatic form creation. It should help.

PHP Code:
// $db is an instance of MySQLi
$db_configuration = array();

if (
$current_tables $db->query("SHOW TABLES")) {
  if (
$current_tables->num_rows 0) {
    while (
$a_table $current_tables->fetch_assoc()) {
      
$table_name current($a_table);
      if (
$table_columns $db->query("SHOW COLUMNS FROM ".$table_name)) {
        while (
$a_column $table_columns->fetch_assoc()) {
          
$db_configuration[$table_name][$a_column['Field']] = $a_column['Type'];
        }
      }
    }
  }

__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-14-2009, 09:04 PM Re: Is there a way to recognize enum/set data type?
Average Talker

Posts: 17
Trades: 0
Thanks for your input. I think I now have the answer to my question. A code like this will return the enum values for a given field either as string or as array:

function getEnumsAsString($table,$field)
{
$query="SHOW COLUMNS FROM " . $table . " LIKE '" . $field . "'";
$result=mysql_query($query);
$enumvals = mysql_result($result, 0, "Type");
//echo "Enum values: $enumvals<br><br>";
return $enumvals;
}
function getEnumsAsArray($table,$field)
{
$str=$this->getEnumsAsString($table,$field);
$str1=preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$str); //echo $str1;
$options=explode("','",$str1); //print_r($options);
return $options;
}
DownWithBugs is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Is there a way to recognize enum/set data type?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22983 seconds with 12 queries