Link to home
Start Free TrialLog in
Avatar of Dany Balian
Dany BalianFlag for Lebanon

asked on

db connection error catch

dear friends,

i'm new to php.. i'm trying to connect to a database (mysql) using php.

for this purpose i've created a file called db.php that i am including in my pages where i need db connectivity.
i've shut off my db engine, and i've opened one of my pages...
and i'm getting an error on the top of the page saying...

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in c:\Inetpub\wwwroot\bimpos\db.php on line 2
Could not connect: Can't connect to MySQL server on 'localhost' (10061)

the error being on top, it is very ugly... this is why i thought to put the error in a variable, and then when i need db connection, i'll check if that variable contains an error, then display the error msg in a nice layout...

what am i doing wrong??

i appreciate ur help..

db.php
---------------------------------
<?php
$con = mysql_connect("localhost","myuser","pass");
if ($con) {
      $db= mysql_select_db("testdb");
      if (!$db) {$dberror=mysql_error(); }
}
else
{
      $dberror='Could not connect: ' . mysql_error();
}
?>

cheers,

dan
ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It would be your USERNAME and PASSWORD...

One thing to check, if you are in a SHARED HOSTING environment (ie. You are using cPanel or some other manager (ensim, plesk)) etc...

Check that the USERNAME and PASSWORD are right.
OFTEN, the username WONT be what you requested it to be...
They often prepend YOUR username to the MySQL usernames.

ie.
You login to cPanel with: "hello"
You make a new MySQL user: "john"

The actual username for MySQL will be:
"hello_john"

Any luck with that?
Avatar of Dany Balian

ASKER

sorry neester, that has nothing to do with what i want...
my connection is successful
but i am error proofing my site, so that: if the db is down, my site wont have a bad look...
raven, ur solution works perfectly...

i just want to know something... will the error_reporting disable all errors on the page??? or on all pages?? or just after the syntax???

thanks for the answer...

cheers,

dan
Ahhhh I see!
Sorry!!!

Totally misread your question!

You can just turn on ERROR Logging, and not have it display any errors to the page.
That will still report errors for you - just wont disrupt your page.

:)

Look here for more info:

http://pl.php.net/manual/en/ref.errorfunc.php#ini.display-errors
> I just want to know something... will the error_reporting disable all errors on the page??? or on all pages?? or just after the syntax???
from thhe point of call, untill next error_reporting() call is done. So in fact You can
$old = error_reporintg(E_ERROR);
#do something here, print errors only.
error_reporintg($old);
Please fix my typos...