我的php脚本在执行返回零(0)行的mysql select查询后停止处理和输出

Below is my code. When i try to insert a user which already exists, the query returns one row and the script work fine. but when the query returns zero row , the script stops at the point commented as "STOPS HERE" in the code below. Please I don't really understand the problem and I'm new to php. Even the echo statement outside the ifs are not executed.

<?php
session_start();
$_SESSION["error"] = "";
include "dbconnection.php";

$_SESSION["validUser"] = 4; 

$validUser = $_SESSION["validUser"];


extract($_POST);

if($courseSession == ""){
    echo "You must Select a course session ";   
}
else {
    $query = "SELECT * FROM courses WHERE student = $validUser AND course='$courseSession'";
    $result = mysql_query($query, $db)  or die(mysql_error($db));

    if(!$result){
        echo " no results have been returned testing "; // used for testing
    }
    else {
        echo " Results returned but don't understand  
 Number rows = ";   // used for testing
        echo mysql_num_rows($result);

    } //STOPS HERE, here is where the script stops after query above returns 0 row.
    $check = mysql_num_rows($result)   or die(mysql_error($db));

    echo " after check course ";
    if($check){

        echo "You have already registered for this session "; 


    }

    else if(!$check){

        $query = "INSERT INTO courses
                            (course, student, groupId)

                        VALUES
                            ('$courseSession', '$validUser', '$group')";

        $res = mysql_query($query, $db) or die(mysql_error($db));
        if(!$res){
            echo "Error Registering Course ";
        }
        else{
            echo "Successfully registered for this session ";
        }
    }
}


echo " TESting testing ";


?>

There is a problem in your code with this line:

$check = mysql_num_rows($result) or die(mysql_error($db));.

When this runs, mysql_num_rows($result) returns 0, which evaluates the same as false in this case, and that is why die() is executed and nothing runs afterwards.

Try the following which might assist as an alternative:

$query = "SELECT Count(*) as ResultVal FROM courses WHERE student = $validUser AND course='$courseSession'";
$result = mysql_query($query, $db)  or die(mysql_error($db));

if($result["ResultVal"] > 0){
       echo "You have already registered for this session "; 
   }

    else if($result["ResultVal"] = 0){ //OR just else

        $query = "INSERT INTO courses

        //rest of the code

try $check = mysql_num_rows($result) or die("hi control is here"); instead of $check = mysql_num_rows($result) or die(mysql_error($db));

If it prints "hi control is here" then this confirms that die is the problem.