Solved

db connection error catch

Posted on 2006-11-12
6
469 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

738 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