php mysqli按变量搜索不起作用?

I am trying to get data from database but the variable $dept is not working in query . the queries result1, result2 and result3 are not returning any results while result4 is working correctly. the variable $dept is also correct as when i print it's value it print (computer) but in query it's not working. help me please

<?php
                if (isset($_POST['select_course'])) {
                    $dept = $_POST['department'];
                    $session = $_POST['session'];
                    $year = $_POST['year'];
                    $lab = $_POST['lab'];
                    $s_type = $_POST['s_type'];
                    $semester = $_POST['semester'];
                    $credit_h = $_POST['credit_h'];

                    $result1 = mysqli_query($con, "SELECT * FROM departments WHERE `name` = '$dept'");
                    $result2 = mysqli_query($con, "SELECT * FROM departments WHERE `name` = '".$dept."'");
                    $result3 = mysqli_query($con, "SELECT * FROM departments WHERE `name` =".mysqli_real_escape_string($dept));

                    $result4 = mysqli_query($con, "SELECT * FROM departments WHERE `name` = 'computer'");

                    while ($row = mysqli_fetch_array($result1)) {

                            echo $row['name'];
                      }                            
                }
                ?>
if (isset($_POST['select_course'])) {
    if ( !isset($_POST['department']) ) {
        trigger_error('missing POST parameter "department"', E_USER_ERROR);
    }
    $query = sprintf("SELECT * FROM departments WHERE `name`='%s'",  mysqli_real_escape_string($con, $_POST['department']));
    $result = mysqli_query($con, $query);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    else {
        while ( $row=mysqli_fetch_array($result) ) {
            echo $row['name'], "
";
        }
    }
}

if this doesn't produce any results, try

if (isset($_POST['select_course'])) {
    if ( !isset($_POST['department']) ) {
        trigger_error('missing POST parameter "department"', E_USER_ERROR);
    }
    $query = sprintf("SELECT Count(*) FROM departments WHERE `name`='%s'",  mysqli_real_escape_string($con, $_POST['department']));
    $result = mysqli_query($con, $query);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    else {
        $row = mysqli_fetch_array($result);
        echo '# of matching records: ', $row[0], "
";
    }
}

small self-contained example:

<?php
$con = new mysqli("localhost", "localonly", "localonly", "test");
if ($con->connect_errno) {
    trigger_error('connect failed', E_USER_ERROR);
}
$con->query('CREATE TEMPORARY TABLE soFoo (`name` VARCHAR(32))');
$con->query("INSERT INTO soFoo (`name`) VALUES ('depa'),('depb'),('depc')");
$_POST = ['select_course'=>'1', 'department'=>'depb']; // <- it's only an example

if (isset($_POST['select_course'])) {
    if ( !isset($_POST['department']) ) {
        trigger_error('missing POST parameter "department"', E_USER_ERROR);
    }
    $query = sprintf("SELECT * FROM soFoo WHERE `name`='%s'",  mysqli_real_escape_string($con, $_POST['department']));
    $result = mysqli_query($con, $query);
    if ( !$result ) {
        trigger_error('query failed', E_USER_ERROR);
    }
    else {
        while ( $row=mysqli_fetch_array($result) ) {
            echo $row['name'], "
";
        }
    }
}