Detect db cell type - WORKS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Detect db cell type - WORKS

Post 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?
Last edited by psurrena on Fri Dec 12, 2008 9:16 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Detect db cell type

Post by requinix »

Might depend on the database.

For MySQL have a look at mysql_fetch_field.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: Detect db cell type -WORKS!

Post 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 />";
}
 
Post Reply