error trap
Moderator: General Moderators
error trap
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);
?>
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
Are you sure that root does not contain password
Re: error trap
dude... just dont go with that thing....ramya123 wrote:Are you sure that root does not contain password
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
You want a try->catch statement.
Something like:
EDIT: Was actually surprised this worked first time 
Cheers,
Darkzaelus
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>");
Cheers,
Darkzaelus
Re: error trap
Or:
Some thing to think about:
- why do you put $user and $pwd in quotes?
- why do you use root user to access your DB?
Code: Select all
if(!$conn=@mysql_connect ($host, $user, $pwd))
die('<font color="red">~ Database Connection Error [Plz inform ADMIN] ~</font>');- 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
That is indeed a much better way of doing it.
Cheers, Darkzaelus.
Cheers, Darkzaelus.
Re: error trap
VladSun wrote:Or:Some thing to think about:Code: Select all
if(!$conn=@mysql_connect ($host, $user, $pwd)) die('<font color="red">~ Database Connection Error [Plz inform ADMIN] ~</font>');
- 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
I don't need tokanchan wrote:did u tried that yourself?
Perhaps, it's because you didn't use the same approach with mysql_select_db() ...kanchan wrote:i get error when i changed database name... it gets some error..
Putting a single variable in quotes doesn't make any sense ...kanchan wrote:and isnt it safe to put $user and $pwd in quotes or what?
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
Re: error trap
Perhaps, it's because you didn't use the same approach with mysql_select_db() ...kanchan wrote:i get error when i changed database name... it gets some error..
i am new in php so could you please help me out for that mysql_select_db() things??