Page 1 of 1

Detect db cell type - WORKS

Posted: Thu Dec 11, 2008 5:22 pm
by psurrena
Is it possible to detect the cell type in your database?

I thought it would be smart if you could output form elements based on the cell type. So if the cell type is text then you would get <textarea></textarea> and if it was varchar then it would be <input type="text" />.

Is this possible?

Re: Detect db cell type

Posted: Thu Dec 11, 2008 10:28 pm
by requinix
Might depend on the database.

For MySQL have a look at mysql_fetch_field.

Re: Detect db cell type -WORKS!

Posted: Fri Dec 12, 2008 8:35 am
by psurrena
It works! Thank you.

A bit of this is from php.net

Code: Select all

 
$sql="select * from people";
$result = mysql_query($sql) or die (mysql_error());
while($meta = mysql_fetch_field($result)){
    echo $meta->name.": ";
    if($meta->type=="string"){
        echo '<input type="text" name="'.$meta->name.'"';
        if($meta->max_length <= '5'){
            echo ' size="'.$meta->max_length.'"';
        }
        echo " />\n";
    }
    if($meta->type=="blob"){
        echo '<br /><textarea name="'.$meta->name."\" /></textarea>\n";
    }
    echo "<br />";
}