simcoweb Posted August 22, 2006 Share Posted August 22, 2006 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 profileif ($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 10Warning: 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 https://forums.phpfreaks.com/topic/18271-trying-to-get-if-statement-to-work-with-query-results/ Share on other sites More sharing options...
Corona4456 Posted August 22, 2006 Share Posted August 22, 2006 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 profileif ($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 https://forums.phpfreaks.com/topic/18271-trying-to-get-if-statement-to-work-with-query-results/#findComment-78439 Share on other sites More sharing options...
simcoweb Posted August 22, 2006 Author Share Posted August 22, 2006 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 10Warning: 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 https://forums.phpfreaks.com/topic/18271-trying-to-get-if-statement-to-work-with-query-results/#findComment-78441 Share on other sites More sharing options...
Corona4456 Posted August 22, 2006 Share Posted August 22, 2006 Try this for your query instead:[code]$sql="SELECT * FROM `members` WHERE `name` = '$name'";[/code] Link to comment https://forums.phpfreaks.com/topic/18271-trying-to-get-if-statement-to-work-with-query-results/#findComment-78447 Share on other sites More sharing options...
simcoweb Posted August 22, 2006 Author Share Posted August 22, 2006 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! Link to comment https://forums.phpfreaks.com/topic/18271-trying-to-get-if-statement-to-work-with-query-results/#findComment-78449 Share on other sites More sharing options...
Corona4456 Posted August 22, 2006 Share Posted August 22, 2006 n/p Link to comment https://forums.phpfreaks.com/topic/18271-trying-to-get-if-statement-to-work-with-query-results/#findComment-78451 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.