Jump to content

Trying to get 'IF' statement to work with query results


simcoweb

Recommended Posts

Ok, this should be simple. I've seen it a dozen times but can't find the right reference to it. I want to display a message if no results show from a database query. So.. 'IF' no results then 'display this message' ... 'ELSE' display this HTML for the results.

Here's my code snippet for this part of the script:

[code]mysql_connect($dbhost, $dbuser, $dbpass) or die('Database has gone bye bye');
mysql_select_db($dbname) or die('Where oh where is that database');
$sql="SELECT * FROM 'members' WHERE 'name' = '$name'";
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
//$num = mysql_num_rows($results);
$num_rows = mysql_result($results, 0, 0);
//start HTML code for display of profile
if ($num_rows == '0') {
echo "<font class='bodytext'>'We are sorry. That profile is unavailable at this time. Please select another.<br />";
} else {
echo <<<HTML[/code]

Getting this error:

[quote]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/wwwxxxx/public_html/profile.php on line 10

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home2/wwwxxxx/public_html/profile.php on line 12[/quote]
Link to comment
Share on other sites

OK... I'm confused as to why you aren't using mysql_num_rows().

so you would do:
[code]
mysql_connect($dbhost, $dbuser, $dbpass) or die('Database has gone bye bye');
mysql_select_db($dbname) or die('Where oh where is that database');
$sql="SELECT * FROM 'members' WHERE 'name' = '$name'";
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
// Change is here
$num_rows = mysql_num_rows($results);
//start HTML code for display of profile
if ($num_rows == 0) {
echo "<font class='bodytext'>'We are sorry. That profile is unavailable at this time. Please select another.<br />";
} else {
echo <<<HTML
[/code]
Link to comment
Share on other sites

Hi: I tried that along with a few other combinations. I just took your code and pasted it over what I had. It displays the 'if' echo statement about the profile but also still displays these errors:

[quote]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/wwwxxxx/public_html/profile.php on line 10

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/wwwxxxx/public_html/profile.php on line 12
'We are sorry. That profile is unavailable at this time. Please select another.[/quote]

View it at: [url=http://www.plateauprofessionals.com/profile.php]http://www.plateauprofessionals.com/profile.php[/url]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.