I had a problem where I couldn't insert data into a table because the columns were not named correctly. They were simply column1, column2, etc. Instead of Date, Address, City, etc. I did not design this database and there is no way for me to change how these tables are made so I decided to insert the names of the columns in the first row below them. Now I have a corresponding piece of data to link them together. So now I am making an array of all the column names, and an array of all the values in row 1 so I can combine them into one associative array. I will later use this as a key for my insert statement.
That may of been confusing so here is what my table looks like
id | column1 | column2 | column3
1 | Date | Address | City
However the next time it could have more columns, with different names so it would look like this
id | column1 | column2 | column3 | column4 | column5
1 | Apple | Orange | Tomato | Pear | Banana
Now here is the code I'm using to try and make my Associative Array:
$col_keys = array();
$col_values = array();
$columns = "SELECT column_name FROM information_schema.columns WHERE table_name = 'mytablename'";
$columns_info = "SELECT * FROM 'mytablename' WHERE 'id' = '1'";
if ($query_run11 = mysql_query($columns)) {
if (mysql_num_rows($query_run11) == NULL) {
$response["success"] = 0;
echo $response;
} else {
while ($row = mysql_fetch_assoc($query_run11)) {
$col_keys['key_name'] = $row['column_name'];
}
}
}
if ($query_run12 = mysql_query($columns_info)) {
if (mysql_num_rows($query_run12) == NULL) {
$response["success"] = 0;
echo $response;
} else {
while ($row = mysql_fetch_assoc($query_run12)) {
$col_values['value_name'] = $row['column_name']; // PROBLEM
}
}
}
$final = array_combine($col_keys, $col_values);
//echo '<pre>'; print_r($final); echo '</pre>
As commented in the code the line below is a problem.
$col_values['value_name'] = $row['column_name'];
It's a problem because 'column_name' is not the name of the column. Obviously that was the whole problem to begin with, there's no way to know what the column name is. It could be column1 - column 67.
So my question is how would I do this?
Also if there is an easier way to do all of this I'm all ears. This is a poor solution but it was the best I could come up with so far.
EDIT
There seems to be a lot of confusion on my overall goal. It's understandable as this database design sucks and its a complex problem. The column names are actually the value of the question. The rows underneath them are the answers to those questions. So eventually this is my goal. I will have an Associative Array that looks like this
column1 => Date
column2 => Address
column3 => City
...
That way I can write an INSERT that inserts 06-22-2013 (the answer to Date) into column1 because column1 => Date.
If more explanation is needed I will add to this.
$row['column1'];. If I'm wrong let me know. Also the whole reason behind all of this is these tables are full of questions, and the questions will be different each time. So what the column names should be will be different each time. Next time the table might have column names of "apple, banana, pear". Which is why I need to create this associative array each time before I make my insert statement otherwise I will never know where to put the values$row['column' . $i], providing you know when to stop, but see my answer down also.