I’ve got the following code that I can’t seem to get working:
function drawTable($result)
{
var_dump($result);
echo '<table border = "1" cellpadding="10">';
echo '<tr>
<th>Album</th>
<th>Tracks</th>
<th>Price</th>
<th>Purchase</th>
</tr>';
foreach($result as &$album) {
$albumID = $album['Album_ID'];
var_dump($albumID);
//echo '<tr><td><img src="' . $resultRow['Image_URL'] . '" width="200px" height="200px"> </td>';
$tracksQuery = "SELECT Track_Title FROM Track WHERE (Album_ID = '$albumID')";
var_dump($tracksQuery);
$DBConnector2 = new DBConnector();
$tracks = $DBConnector2->getSQL($tracksQuery);
var_dump($tracks);
while ($trackTitle = mysqli_fetch_array($tracks)) {
echo $trackTitle['Track_Title'] . '<br />';
}
}
echo '</table>';
}
The var $result is a list of Album_IDs. I want to iterate over this using a foreach and also to run a query to get its associated tracks.
The var_dump gives:
object(mysqli_result)#4 (0) { }
In addition, the SQL I’m using for $result is
SELECT *
FROM Album
ORDER BY Album.Album_Name ASC
Very basic. You can see the other SQL use in the loop.
What am I doing wrong? Thanks.
I assume that $result is the return of a mysql_query function. You can’t do that as it is still a reference to a mysql resultset, not the real mysql result.
Instead of using the foreach iteration, use a while loop like the following:
I’m not so sure whether in the first place you may pass a mysql result reference in a function but you can give it a try 🙂