errors and includes

A

Anonymous

Guest
Hello,

I have an events calendar on my home page and I am trying to include a "nice" error message instead of the stuff that php puts out.

I have an include that is set up like this

Code:
<?php
error_reporting(0);
// DB Connect - Spe-Le-Yai Events
DEFINE (DB_USER, "xxxxx");
DEFINE (DB_PASSWORD, "xxxx");
DEFINE (DB_HOST, "xxxxx");
DEFINE (DB_NAME, "xxxxx");

$db = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('We are experiencing technical difficulties, we will fix this immediately');
mysql_select_db(DB_NAME) or die ('We are experiencing technical difficulties, we will fix this immediately');
?>

Then in my page I have the following

Code:
<?php require ('/path/to/include/src/php/dates2rem.php'); ?>

and the include to display the info from the db
Code:
<div class="dates2rem"><strong>Next Gerneral Lodge Meeting:</strong><br />
<?php // Next General Lodge Meeting
$result = mysql_query("SELECT * FROM events WHERE title = 'gen' AND dbdate >= now() ORDER BY dbdate LIMIT 1", $db);
printf(mysql_result($result,0,date)); ?>
<br />
<br />
<strong>Next Executive Committee Meeting:</strong><br />
<?php // Next Exec Meeting
$result = mysql_query("SELECT * FROM events WHERE title = 'exec' AND dbdate >= now() ORDER BY dbdate LIMIT 1", $db);
printf(mysql_result($result,0,date)); ?>
<br />
<br />
<strong>Next Ordeal:</strong>
<?php // Next Ordeal
$result = mysql_query("SELECT * FROM events WHERE title = 'ordeal' AND dbdate >= now() ORDER BY dbdate LIMIT 1", $db);
printf(mysql_result($result,0,date)); ?>
<br />
<br />
<strong>Next Brotherhood Ceremony:</strong><br />
<?php // Next Brotherhood Ceremony
$result = mysql_query("SELECT * FROM events WHERE title = 'broth' AND dbdate >= now() ORDER BY dbdate LIMIT 1", $db);
printf(mysql_result($result,0,date)); ?>
</div>

When I change the host name of the mysql server it supresses the php errors and displays the error that I specified in die()

When I load the home page instead of the error showing up inside the div that I want it to I only get a blank page with the error text.

I need the error to show up inside the div instead of not rendering the whole page.

I know the code could be made more efficient, Ill work on that later as my skills increase.

Any suggestions would be great.
 
That usually happens when there's an error connection to the database.
Because the Mysql connection code is the first in your code right ?
And if something goes wrong it will terminate the code execution and perhaps first of load even your page style (css)!

As i see you only made the 'error handling' for the MySQL connect and select_db function and probaly you'll get more query execution errors than the this ones.

I strongly recommend to use the die() function in your mysql_query statements too.

You can also make a function say MyDie... like:
Code:
<?php

function MyDie($msg_text, $error_line = false, $error_file = false, $error_sql = false) {

    print '<div>' . $msg_text . "\n";
    
    if ( (!isset($error_line) || ($error_line != '')) && (!isset($error_file) || ($error_file != '')) ) {
        echo '<p>Line: ' . $error_line . '<br />' . "\n";
        echo 'File: ' . $error_file;        
        
        if (isset($error_sql) || ($error_sql != '' )) {
            echo '<br />' . "\n";
            echo 'SQL: ' . $error_sql;
        }

        echo '</p></div>' . "\n";
    } 	 
    exit;
}

?>
So you will use it like:
Code:
<?php

$query = "INSERT INTO table VALUES('1', '2', '3')";

mysql_query($query) or MyDie('Oh.. i couldn't insert that :(', __LINE__, __FILE__, $query);

// Or...

mysql_query($query) or MyDie('Oh.. i couldn't insert that :(');

?>
 
Back
Top