相同的代码适用于localhost,但在服务器上不起作用

I am going to insert some data into database. Before inserting into database, it will check whether the record is exist or not by using mysqli_num_rows. Below is my part of code

$agentSQL = 'SELECT * FROM members WHERE agent_no = "'.$agentNo.'" and id = "'.$agentMemberID.'"';

if (!$agentChk = mysqli_query($dbCon, $agentSQL)) {
    echo "<script>alert('Error: %s
', mysqli_error($dbCon));</script>";
    exit();
}

$agentChkrow = mysqli_num_rows($agentChk);

if ($agentChkrow != 0) {
  //insert data
} else {
  //record exist
}

It has several types of agent, eg. USA agent, Fr agent, Kr agent. So that it will be look like:

if($_POST['type'] == "USA agent"){
  //above code
} else if ($_POST['type'] == "Fr agent"){
  //above code
} else if ($_POST['type'] == "Kr agent"){
  //above code
} else {
  //blablabla
}

And the current problem is the entire code is worked on localhost, and only the part of Fr agent didn't work. When I get the error message from the script and the error is

MySQL server has gone away

also it will get Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in error without using or die(mysqli_error($dbCon)

I've searched some of solution by this error message. But it seems not relevant to wait_timeout, interactive_timeout and max allowed packet since only a part of code didn't work and the remaining code work normally. How can I solve this problem?