Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

db connection error catch

Posted on 2006-11-12
6
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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