dflow Posted May 25, 2010 Share Posted May 25, 2010 i would like to create reports that will be generated automatically depending on the query paramaters if i have a query like: SELECT contact_form.RequestID, contact_form.CustomerLastName ,contact_form.CityID,city_list.CityName FROM contact_form,city_list WHERE RequestID=8023 and city_list.CityID=contact_form.CityID how would i extract the fieldnames so i could use them as the report's column names? thatis loop the head row html insert the fieldnames and then loop the results as rows? Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/ Share on other sites More sharing options...
Sergey Popov Posted May 25, 2010 Share Posted May 25, 2010 You can refer either by index of field in recordset, or make an alias for each field. For example: <?php $r = @mysql_query("SELECT contact_form.RequestID, contact_form.CustomerLastName ,contact_form.CityID,city_list.CityName FROM contact_form,city_list WHERE RequestID=8023 and city_list.CityID=contact_form.CityID"); for($i=0;$i<@mysql_num_rows($r);$i++){ $f = mysql_fetch_array($r); echo $f[0].'<br />'; // -- that would print value of 'contact_form.RequestID' echo $f[1].'<br />'; // -- that would print value of 'contact_form.CustomerLastName' // ... etc. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063084 Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 I would suggest against using the error supressor in code (the @). It is better to turn off display_errors when in production and turn it on in development. If you surpress it you cannot see / view the errors when you need to see them. Just a tidbit of information. Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063088 Share on other sites More sharing options...
Sergey Popov Posted May 25, 2010 Share Posted May 25, 2010 premiso, yeah but in some cases this is still useful. For example now when I answered question of TS, I was not sure if the mentioned tables contain any records.. and just to avid comment like "it is not working, it returns error", I added @. Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063093 Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 and just to avid comment like "it is not working, it returns error", I added @. But how would that help him if it does not work and an error is needed to debug it? To each their own, but it is better to know what the error is than to not, imo. The point of the forums is to provide help and not to just push shit under the rug to hide it from people who may not know any better. If you do not want to help him further with an error he gets, you do not have to. But do not provide a solution that will make it harder for him to get help next time. Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063101 Share on other sites More sharing options...
Daniel0 Posted May 25, 2010 Share Posted May 25, 2010 premiso, yeah but in some cases this is still useful. For example now when I answered question of TS, I was not sure if the mentioned tables contain any records.. and just to avid comment like "it is not working, it returns error", I added @. Your code will never return an error if there are no records in the database. It will give you an error if the query fails to execute. Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063104 Share on other sites More sharing options...
Maq Posted May 25, 2010 Share Posted May 25, 2010 premiso, yeah but in some cases this is still useful. For example now when I answered question of TS, I was not sure if the mentioned tables contain any records.. and just to avid comment like "it is not working, it returns error", I added @. Errors should be caught and handled appropriately, not suppressed. Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063178 Share on other sites More sharing options...
Sergey Popov Posted May 25, 2010 Share Posted May 25, 2010 Guys, I apologize if the code I posted above offended someone here. I just wanted to answer exact question posted by TS, not meant to violate forum rules in any way. Now I feel everyone will stop by to blame on me for using @ Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063182 Share on other sites More sharing options...
Mchl Posted May 25, 2010 Share Posted May 25, 2010 Don't use @! Ever! Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063186 Share on other sites More sharing options...
Daniel0 Posted May 25, 2010 Share Posted May 25, 2010 Guys, I apologize if the code I posted above offended someone here. I just wanted to answer exact question posted by TS, not meant to violate forum rules in any way. Now I feel everyone will stop by to blame on me for using @ You didn't violate any rules. I don't think you offended anyone either. Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063218 Share on other sites More sharing options...
Maq Posted May 25, 2010 Share Posted May 25, 2010 Guys, I apologize if the code I posted above offended someone here. I just wanted to answer exact question posted by TS, not meant to violate forum rules in any way. Now I feel everyone will stop by to blame on me for using @ You didn't violate any rules. I don't think you offended anyone either. Nope, no offense taken. We are simply trying to promote good coding practice Sergey Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063224 Share on other sites More sharing options...
Mchl Posted May 25, 2010 Share Posted May 25, 2010 And also having a little bit fun on your expense... hope you didn't get offended either Quote Link to comment https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/#findComment-1063230 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.