Error in my PHP code

software development

#1

Hi, can someone please point out the blatant error in my php script.


$row = mysql_fetch_array($qResult);
$nRows = mysql_num_rows($qResult);

for ($i=0; $i< $nRows; $i++){
$content .= “<a href=”".$row[‘filename’].".mp3">".$row[‘title’]."
Filesize: “.$row[‘size’].” Mb
Track Length: “.$row[‘length’].” mins

";
}

print ($content);

I have two rows in that table, instead of printing the two rows it prints the second row twice. What Have I done?

Any help much apricated,
-Rich.


#2

Try this instead:

[code]while($row = mysql_fetch_array($qResult)) {
$file_name = $row[‘filename’];
$track_title = $row[‘title’];
$file_size = $row[‘size’];
$track_length = $row[‘length’];
$content .= “<a href=”$file_name.mp3">$track_title
Filesize: $file_size Mb
Track Length: $track_length mins";
}

print $content;[/code]I’ve added some line breaks to the $content variable to tidy it up a little bit, but you can alter that to suit your layout needs. By creating the additional local variables, the code is easier to follow - and it means you can plonk them right into you $content string without concatenating all the time. Using WHILE instead of a FOR loop just means it will do the counting for you.


Simon Jessey
Keystone Websites | si-blog


#3

Thanks scjessey!