Jump to content

Dealing with empty sets or empty queries...


Jim R

Recommended Posts

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>';
}

 

 

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.

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.

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
}

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.