Page 1 of 1

error trap

Posted: Wed Sep 24, 2008 1:22 pm
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);
?>

Re: error trap

Posted: Wed Sep 24, 2008 11:57 pm
by ramya123
Are you sure that root does not contain password

Re: error trap

Posted: Thu Sep 25, 2008 12:02 am
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,

Re: error trap

Posted: Thu Sep 25, 2008 7:01 am
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

Re: error trap

Posted: Thu Sep 25, 2008 7:20 am
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?

Re: error trap

Posted: Thu Sep 25, 2008 7:24 am
by Darkzaelus
That is indeed a much better way of doing it.

Cheers, Darkzaelus.

Re: error trap

Posted: Thu Sep 25, 2008 12:18 pm
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?

Re: error trap

Posted: Thu Sep 25, 2008 12:58 pm
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

Re: error trap

Posted: Thu Sep 25, 2008 1:39 pm
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: