Jump to content

[SOLVED] How to check if a table has a column or not?


kernelgpf

Recommended Posts

See- I've got a training center for a bunch of different animals, but these animals don't have the same stats. So, basically, I want all of the animals to be available to train, and if they try to train an animal, I need code that checks if there is a certain column in a table, or if that animal has a specific stat. How would I do this?

 

Thanks.

I don't know how efficient this would be, but you could do this.

 

<?php

$sql = "SELECT * FROM table LIMIT 1";
$result = mysql_query($sql);

$row = mysql_fetch_assoc($result)

foreach($row as $k => $v)
{
  echo "$k<br />";
}
?>

 

I think it would be best to simply keep track of what table you're supposed to be using and know what columns are in it ahead of time.

You may want to rethink your database design as well.  You could just have one table for all animals, and if you have a dog, only the dog columns get used.  If you have a cat, only the cat columns get used.  I would imagine that there's enough overlap between the different types of animals that this would be a good approach.

 

The database we use here at work is like that.  We keep track of who has donated to us, and there is one table that keeps track of all entities that have made donations.  This includes individuals and organizations.  An individual record will have a first name, middle name and last name.  An organization will only have an organization name.  Every record has all four fields, but only the ones that are relevant ever get used.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.