ivalea Posted October 9, 2006 Share Posted October 9, 2006 Hello,I'm trying to pull customers first name and last name from mysql. What I want to do is say if there is no result returned then either echo an error message or redirect to another page. But what I have now , lets the user into the page whether they are found in the table or not. All I'm trying to do is get the customers first and last name but only if their email address is found in the students table. Here is what I have:[code]$result = mysql_query("SELECT customers.customers_firstname, customers.customers_lastname from customers, students where students.students_email = customers.customers_email_address") or die (mysql_error());if(!result){echo 'None';}else{while ($row = mysql_num_rows($result)) { $first .= $row[ 'customers_firstname' ]; $last .= $row[ 'customers_lastname' ]; }}[/code]This returns a server error when running the script... Any thoughts on how I can achive this correctly? Thanks! :) Quote Link to comment https://forums.phpfreaks.com/topic/23447-how-to-tell-if-there-are-no-results-returned-give-an-error/ Share on other sites More sharing options...
Daniel0 Posted October 9, 2006 Share Posted October 9, 2006 [code]if(mysql_num_rows($result)){ while($row = mysql_fetch_assoc($result)) { // do stuff }}else { echo "No such customer";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23447-how-to-tell-if-there-are-no-results-returned-give-an-error/#findComment-106370 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2006 Share Posted October 9, 2006 You need to test whether the number of rows returned by the function mysql_num_rows() is greater than 0.Here's my take on your code:[code]<?php$q = "SELECT customers.customers_firstname, customers.customers_lastname from customers, students where students.students_email = customers.customers_email_address";$result = mysql_query($q) or die ("Problem with the query: $q<br>" . mysql_error());if(mysql_num_rows($result)) echo 'None';else{ while ($row = mysql_fetch_asssoc($result)) { $first .= $row[ 'customers_firstname' ]; $last .= $row[ 'customers_lastname' ]; }}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/23447-how-to-tell-if-there-are-no-results-returned-give-an-error/#findComment-106373 Share on other sites More sharing options...
ivalea Posted October 9, 2006 Author Share Posted October 9, 2006 Thanks much Daniel0! Works great now. :) Quote Link to comment https://forums.phpfreaks.com/topic/23447-how-to-tell-if-there-are-no-results-returned-give-an-error/#findComment-106382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.