Hello, I can't figure out what is wrong with this code. I poseted here in the PHP forum because I believe that is where my problem lies. I have a customError script that extracts the last 5 errors from the database and compares them with the current error that is being thrown. The second sql select statement below, although doesn't make much sense to me is actually working and is extracting the appropiate errors that I need. First I query the database to see if more than 1 records exists inside the database already, if so then I select the last 5 records, store them into an array and then compare them to the current error, and then if the current error does not match then I insert. The code was actually working at one time with the first select statement below. But when I changed over to the second select statement so I can query within that last hour, it inserts the error no matter if it matches or not. So, basically, I was testing the first code with an echo statement and it was working perfectly. The second code as I said is not working correctly. The sql code works as expected by extracting the last 5 errors that I need and also within the last hour. But when I put it all together with php, it doesn't work anymore. Also if something is missing it's just because I haven't included it here such as a bracket or something. It runs fine just doesn't provide the desired results.
These pieces of codes are just snippets from the script which I think is where my problem lies. Thank you all for any help or advice that you can provide. If you need more info, please let me know.
// first sql select works fine when I test with an echo to the page
//the $number here is used to check if more than 1 error already exists in database
else if($number != 0)
{
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("errorfile") or die(mysql_error());
$data = mysql_query("SELECT `errstr`, `time` FROM `errors` ORDER BY `time` DESC LIMIT 5")
or die(mysql_error());
$str = array();
$timer = array();
while($info = mysql_fetch_array( $data ))
{
$str[] = $info[0];
$timer[] = $info[1];
}
if(!in_array($errstr, $str))
{
$SQL = "INSERT INTO `errorfile`.`errors`";
$SQL = $SQL . "(errno, errstr, errfile, errline, time)
VALUES ";
$SQL = $SQL . "('$errno', '$errstr', '$errfile', '$errline', SYSDATE())";
$result=mysql_db_query(errorfile,"$SQL");
//second sql statement works at the sql prompt
// check if more than 1 error exists in database
else if($number != 0)
{
// Connects to your Database
mysql_connect("mysql", "username", "password") or die(mysql_error());
mysql_select_db("errorfile") or die(mysql_error());
/* run query to extract last 5 errors and also within last hour this works for desired results at the sql prompt*/
$data = mysql_query("SELECT TIME, ERRSTR
FROM `errors`
WHERE TIME NOT BETWEEN CURRENT_TIME
AND ( SELECT DATE_SUB( SYSDATE( ) , INTERVAL +1 HOUR ) )
ORDER BY TIME DESC LIMIT 5;")
or die(mysql_error());
//initiate array to store result set
$str = array();
$timer = array();
while($info = mysql_fetch_array( $data ))
{
// store results
$str[] = $info[0];
$timer[] = $info[1];
}
/*check if error not in array above, not sure if I need to check timer array?please advise me here $errstr below is part of customError function and is what I am checking for*/
if(!in_array($errstr, $str))
{
$SQL = "INSERT INTO `errorfile`.`errors`";
$SQL = $SQL . "(errno, errstr, errfile, errline, time)
VALUES ";
$SQL = $SQL . "('$errno', '$errstr', '$errfile', '$errline', SYSDATE())";
$result=mysql_db_query(errorfile,"$SQL");
}