Problems retrieving data for report

Ok, first of all: this is a part of sc I don’t have much experience. I need a report with a few sections containing blocks of data from different tables. There’s no master-detail, so I thought I could add a custom field and apply some html to it. If I enter it by hand it goes well so I tried to do so from the database:


// selecteer alle intakes

$check_sql = "SELECT m.intakeseq, m.intakedate, m.intaketype, m.intakecomm, i.intakeoms ".
	         " FROM MedicalIntake m, intakesoort i where m.DOHMPersnr = [glob_dohmpersnr]".
	         " AND m.intaketype = i.intakesoort";
sc_lookup(rs, $check_sql);
$i=0;
$r = "<br><table>";
while (isset({rs[$i][0]}))     // Row found
{
       $r.= "<tr>";

       $intakeid = {rs[$i][0]};
       $intakedate = {rs[$i][1]};
       $intaketype = {rs[$i][2]};
       $intakecomm = {rs[$i][3]};
       $intaketypeoms = {rs[$i][4]};
		
	$r.='<td>'.$intakeid.'</td>';
	$r.='<td>'.$intakedate.'</td>';	
	$r.='<td>'.$intaketype.'</td>';	
	$r.='<td>'.$intakecomm.'</td>';	
	$r.='<td>'.$intaketypeoms.'</td>';
 
    $r.="</tr>";
	
	$i++;
}

$r .= '</table>';
{test}='dossier<br>'.$r;

The sql statement is correct and generates the needed data. However I get the following error messages when I run the app:

Fout
Undefined variable: intakedate
Fout
Undefined variable: intaketype
Fout
Undefined variable: intakecomm

Now I’m doing something obvious wrong, but I cannot see it. Anybody?

Ah, if I put the code in a control app I get:


dossier<br><br><table><tr><td>0</td><td>2012-09-08</td><td>ZM</td><td>geconverteerd uit DOHM</td><td>Ziekmelding</td></tr></table>

Which is correct. So now I’m double puzzled.
But if I don’t use variables and put {rs[$i][0]} directly in the output then it works like designed. Why oh why.