blackcell Posted February 5, 2008 Share Posted February 5, 2008 Is there a way to determine whether a field in a table exists or not? I can figure out how to determine whether the table exists by checking the mysql directory on xampp but not the fields within. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/ Share on other sites More sharing options...
effigy Posted February 5, 2008 Share Posted February 5, 2008 Visually or code-wise? There's SELECT * FROM table and[tt] DESC table. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-459124 Share on other sites More sharing options...
blackcell Posted February 5, 2008 Author Share Posted February 5, 2008 I want to check without error reporting and without modifying the php.ini Code-Wise by the way Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-459127 Share on other sites More sharing options...
effigy Posted February 5, 2008 Share Posted February 5, 2008 The only way I know of is to try a SELECT field FROM table LIMIT 1 and catch any errors. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-459137 Share on other sites More sharing options...
blackcell Posted February 5, 2008 Author Share Posted February 5, 2008 Any way to suppress the errors onscreen without modifying php.ini? Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-459153 Share on other sites More sharing options...
effigy Posted February 5, 2008 Share Posted February 5, 2008 http://www.php.net/try Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-459172 Share on other sites More sharing options...
fenway Posted February 6, 2008 Share Posted February 6, 2008 If you're using 5.0, you can query the information_schema table. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-459861 Share on other sites More sharing options...
blackcell Posted February 9, 2008 Author Share Posted February 9, 2008 Does it return errors if it does not exist?? Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-462643 Share on other sites More sharing options...
cedre Posted February 9, 2008 Share Posted February 9, 2008 You should take a look at PHP Manual's page about Exceptions as effigy also suggested. (http://www.php.net/try) Does it return errors if it does not exist?? If you catch them properly it won't return any error. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-462680 Share on other sites More sharing options...
fenway Posted February 9, 2008 Share Posted February 9, 2008 Errors? You simply query to see if there is a table with that name... no different than any other "count". Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-462807 Share on other sites More sharing options...
blackcell Posted February 10, 2008 Author Share Posted February 10, 2008 Errors? You simply query to see if there is a table with that name... no different than any other "count". So I am assuming now that there is a command to execute a 'check for existence' query. That is what I meant in the first place. I will try to look around but I am so buried in home work that I have a hard time find the time to dig deeper into this issue for work. I also need to study on the php try. It sounds like it will do something I want to implement in a few programs but I don't understand it from a quick glance. At any rate thank you for your input. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-462966 Share on other sites More sharing options...
PFMaBiSmAd Posted February 10, 2008 Share Posted February 10, 2008 I think this is a case where if you describe what you are trying to accomplish, rather than describing what you are trying to do (sometimes what you are doing doesn't have anything to do with the overall goal), someone can provide the best way of accomplishing it. In this case, why do you need to find if a field exists? A query referencing a column that does not exist will fail and return a FALSE value. As long as your code contains proper logic to test the value returned by the mysql_query() function call and does not blindly reference the nonexistent result set, no php error will be generated, which seems to be what is happening now. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-462981 Share on other sites More sharing options...
blackcell Posted February 10, 2008 Author Share Posted February 10, 2008 OK, I am building a setup/diagnostic routine that will ensure that the required database is setup correctly, and properly named. I do not want to return mysql errors so that the user sees them. Therefore that is what I am trying to accomplish and DO. I don't want lame mysql errors to appear, because it doesn't seem like good design. I just want a field to be highlighted in red saying "`database` doesn't exist. Attempt to repair? <---then they click on this to try to correct the problem. Does that properly define what I am trying to accomplish rather than what I am trying to DO? Plus, I want the general knowledge and best method of approach to this for future reference. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-463136 Share on other sites More sharing options...
trq Posted February 10, 2008 Share Posted February 10, 2008 mysql_list_fields() should help you out. Quote Link to comment https://forums.phpfreaks.com/topic/89606-solved-checking-to-see-if-field-name-exists-within-a-table/#findComment-463275 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.