Page 1 of 1

Testing for no result ?

Posted: Tue Apr 06, 2010 11:07 am
by MiniMonty
Hi all,

I have a little block of code which lists courses that members have signed up for.
It looks like this:

Code: Select all

<?php $id = mysql_real_escape_string($id);
	$id = eregi_replace("`", "", $id);
	$query = sprintf("SELECT c.* FROM students_to_courses stc INNER JOIN courses c ON stc.course_fk = c.course_id WHERE stc.student_fk = '$id'");
	$result = mysql_query($query);


while ($row = mysql_fetch_assoc($result)) {
	$tutor = $row['tutor_fk'];
    $course_name = $row['course_name'];
    $course_descriptoin = $row['course_description'];
	$course_list = '<a href="coursework.php?courseName=' . $course_name .  '"  class="Times19">' . $course_name . ' </span><span class="Times19">     course work page</span> <br /></a>';
	print $course_list;
}

 ?>

having tried a few things and done some research I can't seem to find the right syntax to test for "no result" and post a message like
"you have not signed up for any courses yet".

Any suggestions ?

Best wishes
Monty

Re: Testing for no result ?

Posted: Tue Apr 06, 2010 11:13 am
by AbraCadaver

Code: Select all

mysql_num_rows()

Re: Testing for no result ?

Posted: Tue Apr 06, 2010 4:37 pm
by MiniMonty
Thanks.

I tried it like this:

Code: Select all

$num_rows = mysql_num_rows($result);
	echo "$num_rows Rows\n";
and it works fine when the original code I posted returns a result (it echos "3 rows") but
it doen't echo "0 rows" when no result is returned, in fact it doesn't echo anything at all.
So I'm still struggling to write an "if" statement to react to the user who hasn't signed up to any course.

Any more pointers ?

Best wishes
Monty

Re: Testing for no result ?

Posted: Tue Apr 06, 2010 6:39 pm
by AbraCadaver
Why echo it? Just do:

Code: Select all

if(!mysql_num_rows($result)) {
   echo "No results...";
}
Or whatever.