kernelgpf Posted October 1, 2007 Share Posted October 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71425-solved-how-to-check-if-a-table-has-a-column-or-not/ Share on other sites More sharing options...
MmmVomit Posted October 1, 2007 Share Posted October 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71425-solved-how-to-check-if-a-table-has-a-column-or-not/#findComment-359530 Share on other sites More sharing options...
kernelgpf Posted October 1, 2007 Author Share Posted October 1, 2007 That would require hard coding, which I don't want to do, so I could add more animals later if necessary. Thank you though, that should work. =] Quote Link to comment https://forums.phpfreaks.com/topic/71425-solved-how-to-check-if-a-table-has-a-column-or-not/#findComment-359531 Share on other sites More sharing options...
MmmVomit Posted October 1, 2007 Share Posted October 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71425-solved-how-to-check-if-a-table-has-a-column-or-not/#findComment-359535 Share on other sites More sharing options...
kernelgpf Posted October 1, 2007 Author Share Posted October 1, 2007 Really? I was thinking that would be really slow.. there is a large difference between the animals [columns] in each table, and there are nearly 7,000 of each animal- do you still advise it? Quote Link to comment https://forums.phpfreaks.com/topic/71425-solved-how-to-check-if-a-table-has-a-column-or-not/#findComment-359540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.