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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.