Jump to content

[SOLVED] Finding Column Types


hukadeeze

Recommended Posts

I'm trying to populate an array with all the tables, columns, and column types from a database.

 

I know to get the table names I can use:

 

show tables from database

 

I can then use those results to loop over another query for column names:

 

show columns from $table

 

What I don't know is how to query for specific information about a column. I'm looking for column types and ranges. Such as a text type with a max of 40 chars. I would like to know not only the query string, but also the format in which it will be returned.To put this into context, I will be using this array in a form validation script. I will check that the incoming form variables match the format of the corresponding fields in the database.

 

 

Link to comment
Share on other sites

SHOW FULL COLUMNS was exactly what I was looking for. In case anybody is curious, if you run SHOW FULL COLUMNS on a table these will be your results.

 

If you use:

$row = $result->fetch_row();

 

Then:

 

$row[0] = Column Name

$row[1] = Column Type (Example: varchar(255))

$row[2] = Collation (Example: utf8_bin)

$row[3] = Accept Null Value? (Yes or No)

$row[4] = If column is primary key this value is PRI. If not then there is no value.

$row[5] = There was no value for this in any of the columns in the table I used.

$row[6] = Extras (Example: auto_increment)

$row[7] = Actions (select,insert,update,references)

 

Thanks for the help fenway!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.