Jump to content

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
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.