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]
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]
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]
Hey, thanks for your posts. I referred to another file in my project and used this line:

[code]$sql=("SELECT * FROM members");[/code]

and it worked fine. No more error messages. Just a couple of other bugs to work out. Thanks for the help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.