Retrieve data from bad formatted database
===

I have to make a report from very old mysql database (about year 2003). So the style is bad.
The structure is that table have columns: id, type, field, value. In these fields are all information but for one id there are many entries. If normally data would be stored like this: id, name, speed, service, project etc, then in this table data is stored like: id (used for many rows), type, field (here is placed name, speed, service .... but just one of them), value (value of field 'field').
But I need to combine this table data for each id (where are multiple entries with each id) with table where are connections between tables and then combine with third table (which actually is the same table as first but with different id). I wrote a script but it is awfully slow, PHP cannot execute it in 30 seconds. Maybe some directions in which I should write script?

Table "Base":

    id | type | field | value

Table "Connects":

    master | slave

I get data from table "base" filtering by "type", then getting first row and correct data, then looping while "master" from table "connects" points to "base" item with "type" which I need and get correct data.
And full script which is too slow:

http://paste.php.lv/ce693aee64e9617b509e336b25e3262f?lang=php
