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.
The simple answer should be:
remove the quotes from the $vars. or single quote them. Also remove dots. Like:
As written in the comment above, you should divide and conquer to make your life easier (especially as you write the code while you play around with it in that large function). That does work as easy as:
for example that is just replacing the many duplicate lines where you just need a (numbered) file you put some string in.
But you can also do this with more complex stuff, like the database operation. You probably want to move the error handling out of your sight as well as taking care to connect to the database when needed and a more flexible way to fetch the data. That can be done by moving the (softly deprecated)
mysql_*
functions into one or two classes of its' own, so that it gets out of your sight. That will make it's usage much more easier (which I show first):I called the database class
MySql
as it represents the mysql connection and it works with the old mysql extension. You only need to pass that database object then into the function in your question. Combined with thefile_put
function, it would look like this:Still the
checkin
function is close to being large (12 lines of code already), but is much shorter than your first version because it delegates the work for writing the files and accessing the database. I hope this demonstration is useful. What follows is the full code example: