Sunday 15 June 2014

php - combine two arrays into associative array, problems -


I had a problem where I could not insert data in table because column < / Code> Names were not correct. They were just columns 1, column 2 etc. Instead of date, address, city etc. I did not design this database and there is no way to make this change for me, so I decided that the insert below the column The name of the line . Now I have a piece of data to link them all together. So now I am creating a array of all the values ​​in a array and line 1 of all column names so that I can add them to one associative array id | Column1 | Column2 Column 3 1 | Date | Address City

Although it may have more columns next time, with different names, it looks like

  id | Column1 | Column2 Column 3 | Column 4 Column 5 1 | Apple | Orange | Tomato | Pears | Ken   

Now I am trying to use the code and my associated array :

  $ col_keys = array (); $ Col_values ​​= array (); $ Column = "SELECT column_name information_schema.column from WHERE table_name = 'mytablename'"; $ Columns_info = "Select * FROM 'mytablename' WHERE 'id' = '1'"; If ($ query_run11 = mysql_query ($ column)) {if (mysql_num_rows ($ query_run11) == faucet) {$ response ["success"] = 0; Echo $ reaction; } And {while ($ line = mysql_fetch_assoc ($ query_run11)) {$ col_keys ['key_name'] = $ line ['column_name']; }}} If ($ query_run12 = mysql_query ($ columns_info)) {if (mysql_num_rows ($ query_run12) == faucet) {$ response ["success"] = 0; Echo $ reaction; } And {while ($ line = mysql_fetch_assoc ($ query_run12)) {$ col_values ​​['value_name'] = $ line ['column_name']; // PROBLEM}}} $ end = array_combine ($ col_keys, $ col_values); // resonance '& lt; Pre & gt; '; Print_r ($ last); Echo & lt; / Pre & gt;   

The line below is a problem in the form of comments in the code. $ col_values ​​['value_name'] = $ line ['column_name'];

This is a problem because the name of the column column_name ' is not named. Obviously this whole problem has started, there is no way to know what the column name is, this column can be 1 - column 67.

So my question is how will I do this?

In addition to this if there is an easy way to all this I am all ears It is a bad solution but it was best that I now I can come up with

Edit

There is a lot of confusion for me, the whole goal. This makes sense because this database design is useless and it has a complex problem. The column name is actually the value of the question. The rows below them are the answers to those questions, so ultimately this is my goal. I have a associated array which looks like this

  column1 = & gt; Date2 column & gt; Address column3 = & gt; City ...   

In this way I can write an INSERT which will be 06-22-2013 (date of reply) to column1 Because column1 = & gt; Date .

If more explanation is required then I will add it.

First of all, switch to PDO or MySQLi; see big warnings; This is PDO code:

  $ dbh = new PDO ('mysql: host = localhost; dbname = my_name', $ user, $ pass); $ Stmt = $ dbh- & gt; Query ($ columns_info); $ Result = $ stmt-> Fetch (); $ Col_values ​​= $ Result;   

You can then add them:

  $ final = array_combine ($ col_keys, $ col_values);   

But putting different types of data in the same column ... looks like a problem just populate it like a separate table:

  + ------------ + ------- + | Column_id | Name | + ------------ + ------- +   

each time, and clear it after use column_id May be 1,2,3 ... with AUTO_INCREMENT

No comments:

Post a Comment