0

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");


}
5
Contributors
19
Replies
20
Views
6 Years
Discussion Span
Last Post by MariahGwen
0

on line 29 of first block and line 39 of second block you have: $result=mysql_db_query(errorfile,"$SQL"); a. mysql_db_query() is deprecated. Use mysql_query() instead.
b. Regarding the first argument to the function did you mean $errorfile ?
c. Also, (assuming you "insist" on sticking with mysql_db_query() ) you connect to the db, but you are NOT "receiving/capturing" the connection in $errorfile - ex: $errorfile=mysql_connect(...);

Edited by hielo: n/a

0

Thanks! I'll try the mysql_query() instead. Obviously , I am out of touch with the new practices in sql. Just so you know, I am still using MySQL 3.12 and not 5.x. I need an update for sure. errorfile is the name of my database, and $errfile is an argument in the error function. And the last, what you are saying is besides my string where I say

mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

I need to add a mysql_connect statement also? I'll post the whole script so you can see better what it is I am doing wrong with it, but give me a little while. What gets me is the fact that it is working correctly until I replaced the first sql statement with the second. At first I was just checking the errstr, then I tried to incorporate the time into it, then now this. Thanks again!

on line 29 of first block and line 39 of second block you have: $result=mysql_db_query(errorfile,"$SQL"); a. mysql_db_query() is deprecated. Use mysql_query() instead.
b. Regarding the first argument to the function did you mean $errorfile ?
c. Also, (assuming you "insist" on sticking with mysql_db_query() ) you connect to the db, but you are NOT "receiving/capturing" the connection in $errorfile - ex: $errorfile=mysql_connect(...);

Edited by jpadgett: n/a

0

on line 29 of first block and line 39 of second block you have: $result=mysql_db_query(errorfile,"$SQL"); a. mysql_db_query() is deprecated. Use mysql_query() instead.
b. Regarding the first argument to the function did you mean $errorfile ?
c. Also, (assuming you "insist" on sticking with mysql_db_query() ) you connect to the db, but you are NOT "receiving/capturing" the connection in $errorfile - ex: $errorfile=mysql_connect(...);

Here is the whole function so you can get a better look at the whole messy thing. I would like to work on compacting it a bit maybe with another function or something but that is a whole different matter. I have to get it working first. I'm going to work the changes that you mentioned earlier.....Thank you!

<?php


/*STEPS TO TAKE WITH CUSTOM ERROR HANDLING FUNCTION

1. INITIATE customError Function
2. QUERY DATABASE AND IF LESS THAN 5 RECORDS EXIST, THEN INSERT RECORD AND SEND MAIL.
3. IF 5 OR MORE RECORDS EXIST, THEN CHECK LAST 5 RECORDS TO SEE IF ANY MATCH THE CURRENT ERROR, IF SO THEN CHECK IF CURRENT RECORD MATCHES RECORDS
INSERTED IN PAST HOUR. IF MATCH THEN EXIT, IF NO MATCH, THEN INSERT RECORD AND SEND MAIL.


*/

// *********** STEP #1 (CUSTOMERROR() FUNCTION) BEGIN **********


function customError($errno, $errstr, $errfile, $errline)
{

// ECHO USED FOR TESTING PURPOSES ONLY
$mess1 = "<p><b>SUCCESS:</b><br />1 record inserted.</p>";
$mess2 = "<p><b>ERROR:</b><br />Record was not inserted because the error already exists!</p>";




// STEP #2 (QUERY DATABASE)

    // Connects to your Database
    mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

// BEGIN SELECT ALL RECORDS

 $data = mysql_query("SELECT * FROM errors")
    or die(mysql_error());

// SET $number EQUAL TO 0 AND ADD 1 AFTER EVERY LOOP
// USED TO TRACK AMOUNT OF ERRORS INSIDE THE DATABASE
   
   $number = 0;


// LOOP THROUGH QUERY RESULTS

    while($info = mysql_fetch_array( $data ))
     {

// INCREMENT $NUMBER BY 1
   $number++;

// END WHILE LOOP
     }


// END SELECT ALL RECORDS


// INSERT VERY FIRST ERROR INTO DATABASE
if($number == 0)
{

       $SQL = "INSERT INTO `errorfile`.`errors`";
$SQL = $SQL . "(errno, errstr, errfile, errline, time)
 VALUES ";
$SQL = $SQL . "('$errno', '$errstr', '$errfile', '$errline', SYSDATE())";
$result=mysql_query(errorfile,"$SQL");

// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED
  $data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1")
  or die(mysql_error());

   while($info = mysql_fetch_array( $data ))
    {
$derrno = $info['errno'];
$derrstr = $info['errstr'];
$derrfile = $info['errfile'];
$derrline = $info['errline'];
$dtime = $info['time'];

//TESTING ONLY!!!
//$files[] = $info['errstr'];

//END WHILE LOOP
    }


//******************SEND CURRENT ERROR MESSAGE***************

// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
$message = "$derrno, $derrstr, $derrfile, $derrline, $dtime";

// In case any of our lines are larger than 70 characters,
// we should usewordwrap()
$message = wordwrap($message, 70);

// SEND CURRENT ERROR MESSAGE TO EMAIL
mail('emailaddresshere', 'PHP Errors', $message);

mysql_close();
exit($mess1);



}



// below code is if there is more than 1 error in database already


else if($number != 0)
{

    // Connects to your Database
    mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

// SELECT USED TO EXTRACT LAST 5 MESSAGES IF WITHIN PAST HOUR
  $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());
$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_query(errorfile,"$SQL");


// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED- WHICH IS ALSO THE ONE THAT WAS JUST ADDED
  $data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1")
  or die(mysql_error());

   while($info = mysql_fetch_array( $data ))
    {
$derrno = $info['errno'];
$derrstr = $info['errstr'];
$derrfile = $info['errfile'];
$derrline = $info['errline'];
$dtime = $info['time'];



//******************SEND CURRENT ERROR MESSAGE***************

// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
$message = "$derrno, $derrstr, $derrfile, $derrline, $dtime";

// In case any of our lines are larger than 70 characters,
// we should usewordwrap()
$message = wordwrap($message, 70);

// SEND CURRENT ERROR MESSAGE TO EMAIL
mail('emailaddresshere', 'PHP Errors', $message);

mysql_close();


}


echo "<br /> 1 record inserted";

}
else
{
exit($mess2);
}



// END ERROR FUNCTION
}





//SET ERROR HANDLER
set_error_handler("customError");

// TRIGGER ERRORS WITH CALLS TO NON-EXISTENT VARIABLES -- FOR TESTING ONLY--
echo($ERRORS515250);


  
?>
0

Still not working correctly, I tried your suggestions with no luck.

on line 29 of first block and line 39 of second block you have: $result=mysql_db_query(errorfile,"$SQL"); a. mysql_db_query() is deprecated. Use mysql_query() instead.
b. Regarding the first argument to the function did you mean $errorfile ?
c. Also, (assuming you "insist" on sticking with mysql_db_query() ) you connect to the db, but you are NOT "receiving/capturing" the connection in $errorfile - ex: $errorfile=mysql_connect(...);

0

You've already connected to the database before initiating the 'if' statement so why are you connecting again if number!=0??

0

What error are you receive ? Post the error message. So, we can approach the problem quickly.

0

What error are you receive ? Post the error message. So, we can approach the problem quickly.

I'm not getting any error with this code everything is syntactically correct. My problem is that I am not getting my desired results. My logic is wrong somewhere and I think maybe in the way that I store the results into the array.

0

You've already connected to the database before initiating the 'if' statement so why are you connecting again if number!=0??

You're right! I will change that I have been moving code around cutting and pasting and overlooked it.

0

Anybody else know what I am missing on this above code? My problem is somewhere around the second select block. I'm not sure how to go about storing the results into arrays and then extracting them back out. I only want to insert into database if the error does not exist already. Any help is greatly appreciated. Thank you!

<?php


/*STEPS TO TAKE WITH CUSTOM ERROR HANDLING FUNCTION

1. INITIATE customError Function
2. QUERY DATABASE AND IF LESS THAN 5 RECORDS EXIST, THEN INSERT RECORD AND SEND MAIL.
3. IF 5 OR MORE RECORDS EXIST, THEN CHECK LAST 5 RECORDS TO SEE IF ANY MATCH THE CURRENT ERROR, IF SO THEN CHECK IF CURRENT RECORD MATCHES RECORDS
INSERTED IN PAST HOUR. IF MATCH THEN EXIT, IF NO MATCH, THEN INSERT RECORD AND SEND MAIL.


*/

// *********** STEP #1 (CUSTOMERROR() FUNCTION) BEGIN **********


function customError($errno, $errstr, $errfile, $errline)
{

// ECHO USED FOR TESTING PURPOSES ONLY
$mess1 = "<p><b>SUCCESS:</b><br />1 record inserted.</p>";
$mess2 = "<p><b>ERROR:</b><br />Record was not inserted because the error already exists!</p>";




// STEP #2 (QUERY DATABASE)

    // Connects to your Database
    mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

// BEGIN SELECT ALL RECORDS

 $data = mysql_query("SELECT * FROM errors")
    or die(mysql_error());

// SET $number EQUAL TO 0 AND ADD 1 AFTER EVERY LOOP
// USED TO TRACK AMOUNT OF ERRORS INSIDE THE DATABASE
   
   $number = 0;


// LOOP THROUGH QUERY RESULTS

    while($info = mysql_fetch_array( $data ))
     {

// INCREMENT $NUMBER BY 1
   $number++;

// END WHILE LOOP
     }


// END SELECT ALL RECORDS


// INSERT VERY FIRST ERROR INTO DATABASE
if($number == 0)
{

       $SQL = "INSERT INTO `errorfile`.`errors`";
$SQL = $SQL . "(errno, errstr, errfile, errline, time)
 VALUES ";
$SQL = $SQL . "('$errno', '$errstr', '$errfile', '$errline', SYSDATE())";
$result=mysql_query(errorfile,"$SQL");

// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED
  $data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1")
  or die(mysql_error());

   while($info = mysql_fetch_array( $data ))
    {
$derrno = $info['errno'];
$derrstr = $info['errstr'];
$derrfile = $info['errfile'];
$derrline = $info['errline'];
$dtime = $info['time'];

//TESTING ONLY!!!
//$files[] = $info['errstr'];

//END WHILE LOOP
    }


//******************SEND CURRENT ERROR MESSAGE***************

// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
$message = "$derrno, $derrstr, $derrfile, $derrline, $dtime";

// In case any of our lines are larger than 70 characters,
// we should usewordwrap()
$message = wordwrap($message, 70);

// SEND CURRENT ERROR MESSAGE TO EMAIL
mail('emailaddresshere', 'PHP Errors', $message);

mysql_close();
exit($mess1);



}



// below code is if there is more than 1 error in database already


else if($number != 0)
{

    // Connects to your Database
    mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

// SELECT USED TO EXTRACT LAST 5 MESSAGES IF WITHIN PAST HOUR
  $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());
$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_query(errorfile,"$SQL");


// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED- WHICH IS ALSO THE ONE THAT WAS JUST ADDED
  $data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1")
  or die(mysql_error());

   while($info = mysql_fetch_array( $data ))
    {
$derrno = $info['errno'];
$derrstr = $info['errstr'];
$derrfile = $info['errfile'];
$derrline = $info['errline'];
$dtime = $info['time'];



//******************SEND CURRENT ERROR MESSAGE***************

// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
$message = "$derrno, $derrstr, $derrfile, $derrline, $dtime";

// In case any of our lines are larger than 70 characters,
// we should usewordwrap()
$message = wordwrap($message, 70);

// SEND CURRENT ERROR MESSAGE TO EMAIL
mail('emailaddresshere', 'PHP Errors', $message);

mysql_close();


}


echo "<br /> 1 record inserted";

}
else
{
exit($mess2);
}



// END ERROR FUNCTION
}





//SET ERROR HANDLER
set_error_handler("customError");

// TRIGGER ERRORS WITH CALLS TO NON-EXISTENT VARIABLES -- FOR TESTING ONLY--
echo($ERRORS515250);


  
?>
0

while($info = mysql_fetch_array( $data ))
{

$str[] = $info[0];
$timer[] = $info[1];

}

Check these array. It must be mysql table field name like '$info["field_1"]', '$info["field_2"]'.

0

I'll try it out.

Check these array. It must be mysql table field name like '$info["field_1"]', '$info["field_2"]'.

0

on line 137 is an 'IF' statement. Should this be nested inside the 'else if' on line 113.
Cause at the moment it's inserting regardless of what $number equals.

0

I thought that you may have something there, but I double checked and the if statement is inside the else if block. It is the while loop that ends just before the if code and the elseif block ends just after I send the email. Thanks anyways. I think my problem lies with the fact that I don't nything with the timer array as I am doing with the errstr array. I just initiate it and fill it and that's it. I am not too strong on understanding how to work with arrays. I am going to read up on the matter. At this point I need a break from it to clear my mind then I'll get back to it tomorrow I guess.

on line 137 is an 'IF' statement. Should this be nested inside the 'else if' on line 113.
Cause at the moment it's inserting regardless of what $number equals.

0

The code is actually working as it is now except for the time portion of it. I can compare the error string against the database and either insert if no match or not insert i no match. The time is what I am having problems with. I can't compare against the current time and insert when needed.

Check these array. It must be mysql table field name like '$info["field_1"]', '$info["field_2"]'.

0

Not too strong on arrays either.
I've recently created an admin page that allows you to create new folders and part of that process is to check the contents of an array.
I used:

$arr=array();
$count=0;
$skins=scandir($_SESSION['newdirectory']);
$start=time();
        foreach ($skins as $skin)
           {
           if (is_dir($_SESSION['newdirectory'] . $skin))
              {
               $arr[$count] = $skin;
               $count++;
              }
           }

Just created a count variable to point at the array and fill with the relevant info.
Not sure if that's any good to you.

0

I'll give it a try. Thanks for the help.

Not too strong on arrays either.
I've recently created an admin page that allows you to create new folders and part of that process is to check the contents of an array.
I used:

$arr=array();
$count=0;
$skins=scandir($_SESSION['newdirectory']);
$start=time();
        foreach ($skins as $skin)
           {
           if (is_dir($_SESSION['newdirectory'] . $skin))
              {
               $arr[$count] = $skin;
               $count++;
              }
           }

Just created a count variable to point at the array and fill with the relevant info.
Not sure if that's any good to you.

0

try:

<?php
/*STEPS TO TAKE WITH CUSTOM ERROR HANDLING FUNCTION
1. INITIATE customError Function
2. QUERY DATABASE AND IF LESS THAN 5 RECORDS EXIST, THEN INSERT RECORD AND SEND MAIL.
3. IF 5 OR MORE RECORDS EXIST, THEN CHECK LAST 5 RECORDS TO SEE IF ANY MATCH THE CURRENT ERROR, IF SO THEN CHECK IF CURRENT RECORD MATCHES RECORDS
INSERTED IN PAST HOUR. IF MATCH THEN EXIT, IF NO MATCH, THEN INSERT RECORD AND SEND MAIL.
*/
// *********** STEP #1 (CUSTOMERROR() FUNCTION) BEGIN **********
function customError($errno, $errstr, $errfile, $errline)
{
    // ECHO USED FOR TESTING PURPOSES ONLY
    $mess1 = "<p><b>SUCCESS:</b><br />1 record inserted.</p>";
    $mess2 = "<p><b>ERROR:</b><br />Record was not inserted because the error already exists!</p>";

	// STEP #2 (QUERY DATABASE)
    // Connects to your Database
    mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

//NO need to loop in order to get the number of errors. Just execute a COUNT(*) query
/*
 	// BEGIN SELECT ALL RECORDS

	$data = mysql_query("SELECT * FROM errors") or die(mysql_error());

	// SET $number EQUAL TO 0 AND ADD 1 AFTER EVERY LOOP
	// USED TO TRACK AMOUNT OF ERRORS INSIDE THE DATABASE   
    $number = 0;


	// LOOP THROUGH QUERY RESULTS
    while($info = mysql_fetch_array( $data ))
     {
		// INCREMENT $NUMBER BY 1
   		$number++;

		// END WHILE LOOP
     }
*/
	$result=mysql_query("SELECT COUNT(*) as `total` FROM `errors`") or die( mysql_error());
	$row=mysql_fetch_assoc($result);
	$number=$row['total'];
	// END SELECT ALL RECORDS


	// INSERT VERY FIRST ERROR INTO DATABASE
	if($number == 0)
	{

        $SQL = " INSERT INTO `errorfile`.`errors`";
		$SQL = $SQL . " (`errno`, `errstr`, `errfile`, `errline`, `time`) VALUES ";
		$SQL = $SQL . " ('$errno', '$errstr', '$errfile', '$errline', SYSDATE())";
		$result=mysql_query($SQL);

//Absolutely not. You already have the data in the function parameters. Use the data in those
//variables. No need to burden the db server to get something you already have!
//The only thing you don't have from the function parameters is the time, but you can get
//date from PHP
/*
		// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED
  		$data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1") or die(mysql_error());

   		while($info = mysql_fetch_array( $data ))
    	{
			$derrno = $info['errno'];
			$derrstr = $info['errstr'];
			$derrfile = $info['errfile'];
			$derrline = $info['errline'];
			$dtime = $info['time'];

			//TESTING ONLY!!!
			//$files[] = $info['errstr'];

		//END WHILE LOOP
    	}
*/

		//******************SEND CURRENT ERROR MESSAGE***************

		// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
		$time=date("Y-m-d H:i:s");
		$message = "$errno, $errstr, $errfile, $errline, $time";

		// In case any of our lines are larger than 70 characters,
		// we should usewordwrap()
		$message = wordwrap($message, 70);

		// SEND CURRENT ERROR MESSAGE TO EMAIL
		mail('emailaddresshere', 'PHP Errors', $message);

		mysql_close();
		exit($mess1);
	}
	// below code is if there is more than 1 error in database already
	else// if($number != 0)
	{
    	// Connects to your Database
    	//mysql_connect("mysql", "username", "pass") or die(mysql_error());
    	//mysql_select_db("errorfile") or die(mysql_error());

		// SELECT USED TO EXTRACT LAST 5 MESSAGES IF WITHIN PAST HOUR
  		$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());
        $str = array();
        $timer = array();
		while($info = mysql_fetch_assoc( $data ))
     	{
			$str[] = $info['errstr'];
			$timer[] = $info['time'];
		}

		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_query(errorfile,"$SQL");

/*
			// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED- WHICH IS ALSO THE ONE THAT WAS JUST ADDED
  			$data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1") or die(mysql_error());

   			while($info = mysql_fetch_array( $data ))
    		{
				$derrno = $info['errno'];
                $derrstr = $info['errstr'];
                $derrfile = $info['errfile'];
                $derrline = $info['errline'];
                $dtime = $info['time'];
			}
*/
			//******************SEND CURRENT ERROR MESSAGE***************
			// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
			$time=date('Y-m-d H:i:s');
			$message = "$errno, $errstr, $errfile, $errline, $time";

			// In case any of our lines are larger than 70 characters,
			// we should usewordwrap()
			$message = wordwrap($message, 70);

			// SEND CURRENT ERROR MESSAGE TO EMAIL
			mail('emailaddresshere', 'PHP Errors', $message);

			mysql_close();
			echo "<br /> 1 record inserted";
		}
		else
		{
			exit($mess2);
		}
	}
} // END ERROR FUNCTION

//SET ERROR HANDLER
set_error_handler("customError");

// TRIGGER ERRORS WITH CALLS TO NON-EXISTENT VARIABLES -- FOR TESTING ONLY--
echo($ERRORS515250);

?>
0

Thanks, I'll give it a try and see what happens.

try:

<?php
/*STEPS TO TAKE WITH CUSTOM ERROR HANDLING FUNCTION
1. INITIATE customError Function
2. QUERY DATABASE AND IF LESS THAN 5 RECORDS EXIST, THEN INSERT RECORD AND SEND MAIL.
3. IF 5 OR MORE RECORDS EXIST, THEN CHECK LAST 5 RECORDS TO SEE IF ANY MATCH THE CURRENT ERROR, IF SO THEN CHECK IF CURRENT RECORD MATCHES RECORDS
INSERTED IN PAST HOUR. IF MATCH THEN EXIT, IF NO MATCH, THEN INSERT RECORD AND SEND MAIL.
*/
// *********** STEP #1 (CUSTOMERROR() FUNCTION) BEGIN **********
function customError($errno, $errstr, $errfile, $errline)
{
    // ECHO USED FOR TESTING PURPOSES ONLY
    $mess1 = "<p><b>SUCCESS:</b><br />1 record inserted.</p>";
    $mess2 = "<p><b>ERROR:</b><br />Record was not inserted because the error already exists!</p>";

	// STEP #2 (QUERY DATABASE)
    // Connects to your Database
    mysql_connect("mysql", "username", "pass") or die(mysql_error());
    mysql_select_db("errorfile") or die(mysql_error());

//NO need to loop in order to get the number of errors. Just execute a COUNT(*) query
/*
 	// BEGIN SELECT ALL RECORDS

	$data = mysql_query("SELECT * FROM errors") or die(mysql_error());

	// SET $number EQUAL TO 0 AND ADD 1 AFTER EVERY LOOP
	// USED TO TRACK AMOUNT OF ERRORS INSIDE THE DATABASE   
    $number = 0;


	// LOOP THROUGH QUERY RESULTS
    while($info = mysql_fetch_array( $data ))
     {
		// INCREMENT $NUMBER BY 1
   		$number++;

		// END WHILE LOOP
     }
*/
	$result=mysql_query("SELECT COUNT(*) as `total` FROM `errors`") or die( mysql_error());
	$row=mysql_fetch_assoc($result);
	$number=$row['total'];
	// END SELECT ALL RECORDS


	// INSERT VERY FIRST ERROR INTO DATABASE
	if($number == 0)
	{

        $SQL = " INSERT INTO `errorfile`.`errors`";
		$SQL = $SQL . " (`errno`, `errstr`, `errfile`, `errline`, `time`) VALUES ";
		$SQL = $SQL . " ('$errno', '$errstr', '$errfile', '$errline', SYSDATE())";
		$result=mysql_query($SQL);

//Absolutely not. You already have the data in the function parameters. Use the data in those
//variables. No need to burden the db server to get something you already have!
//The only thing you don't have from the function parameters is the time, but you can get
//date from PHP
/*
		// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED
  		$data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1") or die(mysql_error());

   		while($info = mysql_fetch_array( $data ))
    	{
			$derrno = $info['errno'];
			$derrstr = $info['errstr'];
			$derrfile = $info['errfile'];
			$derrline = $info['errline'];
			$dtime = $info['time'];

			//TESTING ONLY!!!
			//$files[] = $info['errstr'];

		//END WHILE LOOP
    	}
*/

		//******************SEND CURRENT ERROR MESSAGE***************

		// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
		$time=date("Y-m-d H:i:s");
		$message = "$errno, $errstr, $errfile, $errline, $time";

		// In case any of our lines are larger than 70 characters,
		// we should usewordwrap()
		$message = wordwrap($message, 70);

		// SEND CURRENT ERROR MESSAGE TO EMAIL
		mail('emailaddresshere', 'PHP Errors', $message);

		mysql_close();
		exit($mess1);
	}
	// below code is if there is more than 1 error in database already
	else// if($number != 0)
	{
    	// Connects to your Database
    	//mysql_connect("mysql", "username", "pass") or die(mysql_error());
    	//mysql_select_db("errorfile") or die(mysql_error());

		// SELECT USED TO EXTRACT LAST 5 MESSAGES IF WITHIN PAST HOUR
  		$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());
        $str = array();
        $timer = array();
		while($info = mysql_fetch_assoc( $data ))
     	{
			$str[] = $info['errstr'];
			$timer[] = $info['time'];
		}

		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_query(errorfile,"$SQL");

/*
			// QUERY DATABASE AND SELECT CURRENT ERROR RECORD SO IT CAN BE MAILED- WHICH IS ALSO THE ONE THAT WAS JUST ADDED
  			$data = mysql_query("SELECT `errno`, `errstr`, `errfile`, `errline`, `time` FROM `errors` AS `str_error` ORDER BY `time` DESC LIMIT 1") or die(mysql_error());

   			while($info = mysql_fetch_array( $data ))
    		{
				$derrno = $info['errno'];
                $derrstr = $info['errstr'];
                $derrfile = $info['errfile'];
                $derrline = $info['errline'];
                $dtime = $info['time'];
			}
*/
			//******************SEND CURRENT ERROR MESSAGE***************
			// LOAD MESSAGE WITH CURRENT ERROR FROM DATABASE
			$time=date('Y-m-d H:i:s');
			$message = "$errno, $errstr, $errfile, $errline, $time";

			// In case any of our lines are larger than 70 characters,
			// we should usewordwrap()
			$message = wordwrap($message, 70);

			// SEND CURRENT ERROR MESSAGE TO EMAIL
			mail('emailaddresshere', 'PHP Errors', $message);

			mysql_close();
			echo "<br /> 1 record inserted";
		}
		else
		{
			exit($mess2);
		}
	}
} // END ERROR FUNCTION

//SET ERROR HANDLER
set_error_handler("customError");

// TRIGGER ERRORS WITH CALLS TO NON-EXISTENT VARIABLES -- FOR TESTING ONLY--
echo($ERRORS515250);

?>
0

Hi, You can try this out...It may help u..:)
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>

source:php.net

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.