Solved

db connection error catch

Posted on 2006-11-12
6
465 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now