Question

I have to work with tables created by limesurvey. The table structure is always the same and so are the first 9 colums (id|taken|submitdate|...). But the rest of the 200 columns are dynamically named, consisting of the id, a questionset id and the question id (i.e. 445999X180X151702). New tables might be inserted but will (for now) always follow this scheme. I have to create some form of result-sheet trough php. I could reference the columns by number but that seems wrong to me (change in the number of questions is not very likely but who knows). Is there any different way? If not, can I at least create some sort of mapping-file so that when I have to make any changes to the tables that my php application will be easy to update?

Thank you!

EDIT: Unfortunately I have no control over this table structure. This is the way the tables are created by limesurvey and since that is the tool used here I will have to deal with it. I'm looking for the best way to do that :)

Was it helpful?

Solution

I'm not sure what you want the final result to look like.

The metadata table information_schema.columns, though, can help you. This will give you a mapping from the column position (ordinal_position) and the column name (column_name) for each table.

In php, you can access the data for a column by position. You can then look up the column name for that position in that table . . . and do what you want with it.

For instance, you could take all the possible column names for a set of such tables and create the code for a view for them. Something like:

create view vw_AllQuestions as (
    select <column list> -- with NULL values for the columns not in TableA
    from tableA
    union all
    select <column list> -- with NULL values for the columns not in TableB
    from tableB
    . . .
);

OTHER TIPS

While Gordons answer would have worked I was able to get it done a bit easier:

$query  = 'SELECT * FROM lime_tokens_'.$audit.' AS tokens INNER JOIN lime_survey_'.$audit.' AS audits ON tokens.token = audits.token WHERE tokens.completed != "N"';
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_row($result)) {
...
}

this gives me a numbered array and since the structure is the same for all surveys the array is always the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top