I was trying to set up an error handler and exception handler and while they looked good locally they didn't work on my website.
I get the following error messages:
Warning: set_error_handler() expects the argument (on_error) to be a valid callback in /home/omgma/public_html/Fresh_start/test.php5 on line 8
Warning: set_exception_handler() expects the argument (handle_exception) to be a valid callback in /home/omgma/public_html/Fresh_start/test.php5 on line 9
User: Conjurer - Password: XXXXX
Attempting to connect to with bad password database
Fatal error: Uncaught exception 'Exception' with message 'This is the throw exception on line 26: db_connect cound not connect to database: Access denied for user 'omgma_omgmauser'@'localhost' (using password: YES)' in /home/omgma/public_html/Fresh_start/test.php5:26 Stack trace: #0 {main} thrown in /home/omgma/public_html/Fresh_start/test.php5 on line 26
The code I am using as a test follows:
<?php
session_start();
//create short variable names
$username ="Conjurer";
$passwd = "houd1n1";
// include function files for this application
require_once('includes/member_fns.php5');
set_error_handler("on_error");
set_exception_handler("handle_exception");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Test for OMGMA Connection</title>
</head>
<body>
<?php
echo "<p>User: $username - Password: XXXXX </p>";
echo "<p>Attempting to connect to with bad password database</p>";
@ $db= new mysqli("localhost", "omgma_omgmauser", "bad_passwd", "omgma_members") or die ('I cannot connect to the database because: ' . mysql_error());
if (mysqli_connect_errno() > 0)
{
throw new Exception("This is the throw exception on line 26: db_connect cound not connect to database:\n".mysqli_connect_error());
}
$query = "select user_id, last_name, first_name from directory ";
$result = mysqli_query($db, $query);
$num_results = mysqli_num_rows($result);
echo "<p>Number of rows: $num_results</p>";
for ($i=0; $i <10; $i++)
{
$row = mysqli_fetch_assoc($result);
echo "<p>$i: " .$row['user_id'] .$row['first_name'].$row['last_name'] ."</p>";
}
?>
</body>
</html>
The member_fns.php5 includes the line:
require_once("error_fns.php5");
And the error_fns.php5 code is:
<?php
function on_error($num, $str, $file, $line)
{
print "<p><strong>Encountered error: </strong>$num in $file, line $line: $str\n</p>";
}
function handle_exception($exception)
{
echo "<p><strong>Caught exception: </strong>{$exception->getMessage()}\n</p>";
}
?>
So I don't understand:
1) why did the error handler and exception handler fail?
2) Why does the following line NOT generate the on die message that is intended?
@ $db= new mysqli("localhost", "omgma_omgmauser", "bad_passwd", "omgma_members") or die ('I cannot connect to the database because: ' . mysql_error());
Thanks! 😕