I'm building a PHP script that will run monthly, to gather data from different tables and bring it into a new table.
As it stands right now, I am dropping the tables monthly, and re-creating them in this script. The column names consist of the month name and year, and therefore will change monthly.
For example, the table I'm working in right now has a column for november12, december12, january12, all the way through december13.
I need to extract the data from the columns in order to calculate averages, and more importantly, display the data on the page.
My question is how to structure my SELECT query based on these dynamic column names, and then how to put them into variables to display on the page in my WHILE loop?
You can either run a DESCRIBE <table> query to get the table structure, or as long as you have a record, you can just look at the keys of any record that is returned with mysql_fetch_assoc().
By the way, mysql_ functions are deprecated, so you may want to consider moving to the newer mysqli functions.
That said, I would expect that it might make sense to have the date values in a single column in each of the rows instead of in columns for each date. One example of why that might be an advantageous design would emerge if the boss comes in and says, "Give me the last two years data." If the dates are in one column you just change the query. If the dates are in columns by month, you have to rewrite your script.
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
http://us3.php.net/array_keys
Open in new window