PHP file cannot enter some part of code
===

Here is a function in my php file, which is used to serve the request of my android app.

    function checkin($DB, $TechID, $ClientID, $SiteID){
		$dbConnection = mysql_connect($DB['server'], $DB['loginName'], $DB['password']);
		if(!$dbConnection){
			die('Error! ' . mysql_error());
			}
		mysql_select_db($DB['database'], $dbConnection);
		
		$file2 = "C:/wamp/www/file2.txt";
		$data2 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID;
		file_put_contents($file2, $data2);
		
		$result1 = mysql_query("SELECT COUNT(*) FROM Log") or die('Error! ' . mysql_error());
		$query = "SELECT `Type` FROM `Log` WHERE `TechID` = '".$TechID."' ORDER BY LogTime DESC LIMIT 1";
		$file5 = "C:/wamp/www/file5.txt";
		file_put_contents($file5, $query);
		$result2 = mysql_query($query) or die('Error! ' . mysql_error());
		while($row1 = mysql_fetch_array($result1)){
			$count = $row1['COUNT(*)'];
			$file3 = "C:/wamp/www/file3.txt";
			$data3 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID." Count:".$count;
			file_put_contents($file3, $data3);
			while($row2 = mysql_fetch_array($result2)){
				$file4 = "C:/wamp/www/file4.txt";
				$data3 = "ClientID:".$ClientID." TechID:".$TechID." SiteID:".$SiteID." Count:".$count;
				file_put_contents($file4, $data3);
				/*if($row2['Type']!="Checkin"){
					$count = $count+1;
					$Time = date('Y/m/d H:i');
					mysql_query("INSERT INTO Log (LogID, TechID, ClientID, SiteID, LogTime, Type)
								VALUES (".$count.", ".$TechID.", ".$ClientID.", ".$SiteID.", ".$Time.", Checkin)");
				 }else{
					$query2 = "SELECT TechEmail FROM Tech WHERE TechID=".$TechID;
					$result3 = mysql_query($query2) or die('Error! ' . mysql_error());
					$subject = "Please check out";
					$message = "You have forgot to logout from the last site, please check out manually";
					$from = "devadmin@uniserveit.com";
					$header = "Form:".$from;
					while($row3 = mysql_fetch_array($result3)){
						mail($row3['TechEmail'], $subject, $message, $header);
					}
				}*/
			}
		}
	}

you can see that I have hidden some codes, since I am debugging it, I create some files just to see which part of codes cannot be executed. I discover that the program cannot enter the region where file4 should be created. I have seeked out probably the problem is coming from the $query, when it executes, somethimes the mysql will response "unknown table status: TABLE_TYPE", which I cannot understand why.