Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/202824-get-fieldname-from-query/
Share on other sites

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.
  }

?>

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.

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 @.

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.

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.

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.

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.

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 ;)

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.