Testing for no result ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Testing for no result ?

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Testing for no result ?

Post by AbraCadaver »

Code: Select all

mysql_num_rows()
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Testing for no result ?

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Testing for no result ?

Post by AbraCadaver »

Why echo it? Just do:

Code: Select all

if(!mysql_num_rows($result)) {
   echo "No results...";
}
Or whatever.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply