/    Sign up×
Community /Pin to ProfileBookmark

Problem with simple IF statement

Hello,

Can somebody help me out.
I basically want to display the results if the number of rows returned is greater than zero, and if not, echo out a reason why.

However, when using the code below, it doesn’t echo anything even if there are no results.

Any ideas ?

[code=php]
<?php # this file

$page_title = “Pictures”;
include (‘./includes/header.html’);

// Check for a valid picture ID, through GET or POST.
if ( (isset($_GET[‘id’])) && (is_numeric($_GET[‘id’])) ) { // Accessed through picturesarea.php.
$id = $_GET[‘id’];
} elseif ( (isset($_POST[‘id’])) && (is_numeric($_POST[‘id’])) ) {// Form has been submitted.
$id = $_POST[‘id’];
} else { // No valid ID, Kill the script.
echo ‘<h2>Page Error</h2><p class=”error”>This page has been accessed in error. <a href=”picturesarea.php”>Return to ‘Pictures’.</a></p><p><br /><br /></p>’;
include (‘./includes/footer.html’);
exit();
}

require_once (‘../mysql_connect.php’); // Connect to the database

$query2 = “SELECT aname FROM area WHERE id = $_GET[id]”;
$result2 = mysql_query($query2) or trigger_error(“Query: $query2n<br />MySQL Error: ” . mysql_error());
$row2 = mysql_fetch_array($result2);

$query = “SELECT CONCAT(image_name, ‘, ‘, dateUpload), id, user_id FROM user_images WHERE aid = $_GET[id] AND approved=’Y'”;
$result = mysql_query($query) or trigger_error(“Query: $queryn<br />MySQL Error: ” . mysql_error());
echo ‘<p><h2>’ . $row2[aname] . ‘</h2></p>’;

$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {

$query3 = “SELECT user_id, username FROM users WHERE user_id = $row[user_id]”;
$result3 = mysql_query($query3) or trigger_error(“Query: $query3n<br />MySQL Error: ” . mysql_error());
$row3 = mysql_fetch_array($result3);

if ($num > 0) { // If it ran OK, display the records.

echo ‘<p><a href=”picture.php?id=’ . $row[1] . ‘”>’ . $row[‘0’] . ‘</a> – posted by : <a href=”view_profile.php?id=’ . $row3[‘user_id’] . ‘”>’ . $row3[‘username’] . ‘</a></p>’;
}

else { // If it did not run OK.
echo ‘<p class=”error”>There are currently no pictures in the database for the area. Please check back soon</p>’;
}
}

?>

<?

include (‘./includes/footer.html’);

?>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehFeb 06.2006 — That is because the echo is inside the while loop which will only be entered if results are returned.
Copy linkTweet thisAlerts:
@invisionauthorFeb 06.2006 — Sorted.

Thanks once more for your speedy help.
Copy linkTweet thisAlerts:
@bokehFeb 06.2006 — The thing is I can see why it doesn't work but I don't have time right now to try to figure out what is wrong with your logic; I'm working on a project with a tight deadline. You really need to break this down into clear steps. Finish one step before starting the next. And indent your code so it is easy to see where each conditional starts and finishes. Try to make your code look something like the following... It's so much easier to read:[code=php]<?php

if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
echo $row['something'];
}
}
else
{
echo 'no results';
}

?>[/code]
Copy linkTweet thisAlerts:
@invisionauthorFeb 06.2006 — Sorry bokeh.

I did get it working in the end, I edited the above above post few minutes ago.

Thanks again for your help.
×

Success!

Help @invision 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.28,
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,
)...