Hi all, I’m currently developing a PHP/MySQL system using a couple of relational databases and have come unstuck with the following code. I’m assuming that the fix is relatively simple and I’m overlooking it but I just wondered if someone could cast a fresh set of eyes on it and give me a nudge in the right direction?
[code=php]
while ($selRows = mysql_fetch_array($select) ) {
//Find the total number of titles owned
$numTitlesSQL = “SELECT count(copy_no) AS NUM
FROM copies
WHERE rent_title = ‘$selRows[title]'”;
$numTitles = mysql_query($numTitlesSQL) or die (mysql_error());
$totalTitles = mysql_result($numTitles, 0) or die(mysql_error());
//Find the total number of titles rented out
//—- THIS CHUNK APPEARS TO BE THE PROBLEM
$numRentsSQL = “SELECT count(rentals.copy_no)
FROM copies, rentals
WHERE copies.rent_title = ‘$selRows[title]’
AND rentals.copy_no = copies.copy_no”;
$numRents = mysql_query($numRentsSQL) or die (mysql_error());
$totalRents = mysql_result($numRents, 0) or die (mysql_error());
//Find total available titles
$totalAvailable = ($totalTitles-$totalRents);
//Print table row
echo ‘<tr>’;
echo ‘<td><a href=”browse.php?’.$selRows[title].'”>’.$selRows[“title”].'</a></td>’;
echo ‘<td>’.$selRows[“age_rating”].'</td>’;
echo ‘<td>’.$selRows[“format”].'</td>’;
echo ‘<td>’.$totalAvailable.'</td>’;
echo ‘</tr>’;
}
All of the above code runs fine until there is an entry in Copies that doesn’t appear in Rentals. I know that the actual SQL code works OK because I’ve checked it in a GUI system and the result is simply 0 (which was expected), but I can’t understand why the PHP cuts out as soon as it reaches the point where rentals.copy_no = copies.copy_no but no copy_no is present in the Rentals table..
Apologies if this is all a little long winded but any suggestions would be appreciated.
Thanks in advance