/    Sign up×
Community /Pin to ProfileBookmark

PHP/MySQL loop problem

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>’;

}
[/code]

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

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 — At a minimum, you'll have to introduce the following changes:[code=php] $numTitlesSQL = "SELECT count(copy_no) AS NUM
FROM copies
WHERE rent_title = '" . addslashes($selRows['title']) . "'
GROUP BY NUM ";[/code]
And this one too:[code=php] $numRentsSQL = "SELECT count(rentals.copy_no)
FROM copies, rentals
WHERE copies.rent_title = '" . addslashes($selRows['title']) . "'
AND rentals.copy_no = copies.copy_no
GROUP BY rentals.copy_no";[/code]
To use functions like AVG() and COUNT(), you'll have to GROUP BY. See http://dev.mysql.com/doc/refman/4.1/en/group-by-functions.html for more information.

Also, whenever you use strings in a query, you want to make sure that unallowed characters are "escaped".

Fix that problem and see what happens.
Copy linkTweet thisAlerts:
@AranellauthorJan 03.2007 — Thanks for the heads up on the addslashes() but it's not helped the running of the code :S At the moment it's just displaying one row and cutting out.
Copy linkTweet thisAlerts:
@NightShift58Jan 03.2007 — Are you getting an error message?

This is the line that determines the number of iterations through the record set:[code=php]while ($selRows = mysql_fetch_array($select) ) {[/code]So, if the code only cycles through the record set for a single row, it could be that that is all that was selected. It's hard to tell - don't know what's in the table.
×

Success!

Help @Aranell spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...