error trap

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

error trap

Post by kanchan »

i want to trap error in php but dont know whats wrong in here..

whenever username or password is incorrect, it shows like this:
Warning: mysql_connect(): Access denied for user: 'root@localhost' (Using password: YES) in connect.php on line 7
~ Database Connection Error [Plz inform ADMIN] ~


instead of
Database Connection Error [Plz inform ADMIN]

how to do that??

here's my connect.php code:

<?
$user="root";
$pwd="";
$db="merobike";
$host="localhost";

$conn=mysql_connect ($host, "$user", "$pwd") or die

("<font color=\"red\">".'~ Database Connection Error [Plz inform ADMIN] ~'."</font>");
mysql_select_db ($db);
?>
ramya123
Forum Newbie
Posts: 13
Joined: Thu Sep 11, 2008 6:11 am

Re: error trap

Post by ramya123 »

Are you sure that root does not contain password
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: error trap

Post by kanchan »

ramya123 wrote:Are you sure that root does not contain password
dude... just dont go with that thing....

i just wanna remove this thing:

Warning: mysql_connect(): Access denied for user: 'root@localhost' (Using password: YES) in connect.php on line 7

and wanna show : Database Connection Error [Plz inform ADMIN]


thanks,
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: error trap

Post by Darkzaelus »

You want a try->catch statement.
Something like:

Code: Select all

 
try {
    if(!$conn=mysql_connect ($host, "$user", "$pwd"))
        throw new Exception($e);
} catch (Exception $e)
    die("<font color=\"red\">".'~ Database Connection Error [Plz inform ADMIN] ~'."</font>");
 
EDIT: Was actually surprised this worked first time :P


Cheers,

Darkzaelus
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: error trap

Post by VladSun »

Or:

Code: Select all

if(!$conn=@mysql_connect ($host, $user, $pwd))
    die('<font color="red">~ Database Connection Error [Plz inform ADMIN] ~</font>');
Some thing to think about:
- why do you put $user and $pwd in quotes?
- why do you use root user to access your DB?
There are 10 types of people in this world, those who understand binary and those who don't
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: error trap

Post by Darkzaelus »

That is indeed a much better way of doing it.

Cheers, Darkzaelus.
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: error trap

Post by kanchan »

VladSun wrote:Or:

Code: Select all

if(!$conn=@mysql_connect ($host, $user, $pwd))
    die('<font color="red">~ Database Connection Error [Plz inform ADMIN] ~</font>');
Some thing to think about:
- why do you put $user and $pwd in quotes?
- why do you use root user to access your DB?


did u tried that yourself?

i get error when i changed database name... it gets some error..

i need to trap the error when accidently username, password or database is changed...

how to do that??

and isnt it safe to put $user and $pwd in quotes or what?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: error trap

Post by VladSun »

kanchan wrote:did u tried that yourself?
I don't need to ;)
kanchan wrote:i get error when i changed database name... it gets some error..
Perhaps, it's because you didn't use the same approach with mysql_select_db() ...
kanchan wrote:and isnt it safe to put $user and $pwd in quotes or what?
Putting a single variable in quotes doesn't make any sense ...

The manual:
http://bg.php.net/manual/en/language.op ... ontrol.php
There are 10 types of people in this world, those who understand binary and those who don't
kanchan
Forum Commoner
Posts: 80
Joined: Tue Nov 30, 2004 12:03 pm

Re: error trap

Post by kanchan »

kanchan wrote:i get error when i changed database name... it gets some error..
Perhaps, it's because you didn't use the same approach with mysql_select_db() ...

i am new in php so could you please help me out for that mysql_select_db() things??

:wink: :wink: :wink:
Post Reply