Jim R Posted September 6, 2011 Share Posted September 6, 2011 Man, I'm having a rough week with questions--if you're following at home. Basically matching a Coach's first and last name to another table, which if matched will yield information about that coach's team. // or get user by username $current_user = wp_get_current_user(); $current_first = $current_user->user_firstname; $current_last = $current_user->user_lastname; $current_id = $current_user->ID; echo '<div class="contact_form">'; echo $current_first . ' ' . $current_last .' :: '; $query = "SELECT * FROM schools WHERE coachFirst='".$current_first."' AND coachLast='".$current_last."'"; $result = mysql_query ($query); if(!$result) { echo 'There is not a match for your name.'; } else { while($school= mysql_fetch_array($result)) { echo $school['school'] . ' ' . $school['id']; include(ABSPATH ."wp-content/plugins/my-team/form.php"); } echo '</div>'; echo '<div class="roster">'; include(ABSPATH ."wp-content/plugins/my-team/roster.php"); echo '</div>'; } Link to comment https://forums.phpfreaks.com/topic/246515-dealing-with-empty-sets-or-empty-queries/ Share on other sites More sharing options...
Jim R Posted September 6, 2011 Author Share Posted September 6, 2011 Basically what I'm getting with that is the WHILE loop picks up there is no match, but the IF doesn't. So it's not showing the contact_form DIV, but it is show the roster DIV. It's probably something grossly simple that I'm missing or just basically goes over my head. Link to comment https://forums.phpfreaks.com/topic/246515-dealing-with-empty-sets-or-empty-queries/#findComment-1265825 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2011 Share Posted September 6, 2011 if(!$result) { ^^^ That only tests if the query executed with an error of some kind, i.e. a connection problem, a sql syntax error, a mistyped table or column name, ... A query that matches zero rows is a successful query and returns a result resource, that contains zero rows. See mysql_num_rows to determine how many rows a successful query returned. Link to comment https://forums.phpfreaks.com/topic/246515-dealing-with-empty-sets-or-empty-queries/#findComment-1265826 Share on other sites More sharing options...
Jim R Posted September 6, 2011 Author Share Posted September 6, 2011 Awesome. Thanks. I have another one down there, dealing with the same code, different issue. Link to comment https://forums.phpfreaks.com/topic/246515-dealing-with-empty-sets-or-empty-queries/#findComment-1265827 Share on other sites More sharing options...
pein87 Posted September 6, 2011 Share Posted September 6, 2011 You could use the mysql_num_rows function to check if the returned rows is greater then 0. if(mysql_num_rows($result) > 0) { //greater then 0 some rows return lets get some data while($team = mysql_fetch_assoc($result)) { //echo something here using $team['field_name_here'] } } else { //trigger no data error } Link to comment https://forums.phpfreaks.com/topic/246515-dealing-with-empty-sets-or-empty-queries/#findComment-1265828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.