Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9035659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:48:33+00:00 2026-06-16T08:48:33+00:00

I am trying to connect to a MySQL database stored on localhost. Code: $config[‘db’]

  • 0

I am trying to connect to a MySQL database stored on localhost.

Code:

$config['db'] = array( 
    'host' => 'localhost',
    'username' => 'root',
    'password' => 'mypass',
    'dbname' => "dbname"
);

echo $config['db']['host'];

try {
    $conn = new PDO('mysql:host=127.0.0.1;dbname=sfrtv', 'root', 'usbw', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

This code returns the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (10061)' in H:\path\filename.php:12 Stack trace: #0 H:\path\filename.php(12): PDO->__construct('mysql:host=loc....', 'root', 'mypass') #1 {main} thrown in H:\path\filename.php(12) on line 12

Any suggestion on how to correct this?

Thx!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:48:34+00:00Added an answer on June 16, 2026 at 8:48 am

    I am sure you got the code from Alex, in PHP academy.
    Can you try checking your mysql details are the same. Try this, I changed the quotes and white spaces.

       $config['db'] = array( 
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'mypass',
        'dbname' => 'dbname'
        );
    
            echo $config['db']['host']; 
    
            $dbh = new PDO('mysql:host='. $config['db']['host'] .';dbname='. $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
    

    Or check back the tutorial again: http://www.youtube.com/watch?v=XQjKkNiByCk


    And if not, try this way of connecting with the below code, as it will catch the exception and echo out what the problem is.

    try {
        $conn = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'mypass', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    

    The above way of connecting is much safer, because it something goes wrong it will not display your entire file and directory structure to the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to a remote host(CentOS) mysql database from localhost(CentOS) with phpmyadmin
I am trying to connect to a remote mysql database from an iPhone. I
I'm trying to connect to a MySQL 5 database using the MySQL ODBC 5.1
I am trying to connect to a MySQL 5.5 database as follows: public void
I'm trying to write a java program to connect to the same MySQL database
I am trying to connect mysql from within PHP function. The credentials are stored
I am trying to connect to MySQL database over a network. I have installed
I am trying to connect to a remote mysql database but I get the
I'm trying to connect Django to a MySQL database which is accessible through an
I am trying to connect to a remote mysql database using django. The documentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.