TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-09-2009, 01:32 AM   #1 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default Custom Error Messages?

Alright so i know the if and else features stuff but im trying to do something that is different, idk if this should go into the mysql area or not so i just put it here.

I am connecting to my database

PHP Code:
mysql_connect($hostname$username$password) or die(mysql_error()); mysql_select_db($databasename); 
however I am curious how would i go about including like a dberror.html file for the error like vbulletin does. I was trying to find out how they do it but i cant find the file where they connect to the database.

Cheers,
Randy
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 06-09-2009, 01:38 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

How about forwarding the user to a generic error page when an error occurs?
(Please excuse the laziness of adding an @)

php Code:
if (!@mysql_connect('Bad', 'connection', 'details'))
{
    header('location: databaseError.php');
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-09-2009, 01:43 AM   #3 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

It could work but im trying to use as little if statements as possible
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 06-09-2009, 02:09 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,215
Thanks: 90
Wildhoney is on a distinguished road
Default

Okay How about this one, amigo?

php Code:
mysql_connect('Bad', 'connection', 'details') or header('location: databaseError.php');

And what is wrong with if statements?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-09-2009, 02:55 AM   #5 (permalink)
The Contributor
 
etoolbox's Avatar
 
Join Date: Dec 2008
Location: Auckland, NZ
Posts: 25
Thanks: 0
etoolbox is on a distinguished road
Default

You can do something like this:

PHP Code:
@mysql_connect("localhost""mysql_user""mysql_password");
$error mysql_error();
if(
$error) {
    
// do something, including outputting error, logging it etc
}

$mysql_query("...");
$error mysql_error();
if(
$error) {
    
// do something, including outputting error, logging it etc

But of course you're best to either wrap all mysql calls into wrapper functions which check for errors with a common error handling function so you don't have to write mysql_error() all over the place, or use one of the many database abstraction libraries which usually have some form of common error handling function that you can set up.
__________________
Chris Hope's LAMP Blog: http://www.electrictoolbox.com/

Last edited by codefreek : 06-09-2009 at 05:45 PM. Reason: PHP Tags Added :)
etoolbox is offline  
Reply With Quote
Old 06-09-2009, 11:14 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Why not use a wider error/exception handler and have that respond appropriately to these conditions? It'll help centralise all of your error/exception handling so there is no need for ifs and buts all throughout your code, as etookbox mentioned. Within your handler you can look for a database connection error specifically (if you desire) and respond however you like (logging, send an email, print error page, or all of the above).
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 06-10-2009, 01:15 AM   #7 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

Thanks, got it working much appreciated and nothing is wrong with them WildHoney i just have many of them already and yah i have been using that idea throughout my code that etoolbox mentioned to handle login errors etc.. has worked wonders :D
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on Custom Data Storage TheOnly92 Advanced PHP Programming 35 05-21-2009 10:47 AM
Creating custom php extensions with DevCPP? Nor Advanced PHP Programming 6 01-30-2008 03:49 PM
Custom BBCode / WYSIWYG Gareth Absolute Beginners 8 01-08-2008 08:28 PM
Optional Parameters in Custom functions.. Orc General 4 12-29-2007 11:07 PM
Custom Icons Are Back mike.fro News and Announcements 0 02-25-2007 05:43 PM


All times are GMT. The time now is 10:58 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design