Solved

db connection error catch

Posted on 2006-11-12
6
467 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Dany Balian
  • 3
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
ravenpl earned 125 total points
ID: 17928379
put
error_reporting(E_ERROR);
in very beggining of the script. Refer http://pl.php.net/error_reporting
0
 
LVL 11

Expert Comment

by:neester
ID: 17928592
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?
0
 
LVL 11

Author Comment

by:Dany Balian
ID: 17928828
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
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 11

Expert Comment

by:neester
ID: 17928850
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
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 17928852
> 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);
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 17928854
Please fix my typos...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question