Jump to content


Photo

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


  • Please log in to reply
5 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 22 August 2006 - 05:27 AM

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:

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

Getting this error:

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



#2 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 22 August 2006 - 05:48 AM

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

so you would do:
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

What is the CoronaMatrix?

#3 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 22 August 2006 - 06:01 AM

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:

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.


View it at: http://www.plateaupr...com/profile.php

#4 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 22 August 2006 - 06:19 AM

Try this for your query instead:

$sql="SELECT * FROM `members` WHERE `name` = '$name'";

What is the CoronaMatrix?

#5 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 22 August 2006 - 06:24 AM

Hey, thanks for your posts. I referred to another file in my project and used this line:

$sql=("SELECT * FROM members");

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

#6 Corona4456

Corona4456
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts
  • LocationEl Paso, TX

Posted 22 August 2006 - 06:28 AM

n/p
What is the CoronaMatrix?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users