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 8777367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:15:46+00:00 2026-06-13T19:15:46+00:00

I am getting an error: Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [2002]

  • 0

I am getting an error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/content/........

Althought if I do it in one file, it works fine!

My php file:

<?php
require 'includes/dbc.php';

$dbc = new dbc();
$db = $dbc->openDb();
$stmt = $dbc->getAllUsers($db);

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo $row['iduser'];
}
?>

my database class:

<?php

class dbc {

    public $dbserver = '';
    public $dbusername = '';
    public $dbpassword = '';
    public $dbname = '';

    function openDb() {

        $db = new PDO('mysql:host=' . $dbserver . ';dbname=' . $dbname . ';charset=utf8', '' . $dbusername . '', '' . $dbpassword . '');
        return $db;
    }

    function getAllUsers($db) {
        $stmt = $db->query("SELECT * FROM user");
        return $stmt;
    }
}

?>
  • 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-13T19:15:47+00:00Added an answer on June 13, 2026 at 7:15 pm

    In the sample-code you provided, you’re not specifying a database, user, password or host to the database connection string:

    function openDb() {
        $db = new PDO('mysql:host=' . $dbserver . ';dbname=' . $dbname . ';charset=utf8', '' . $dbusername . '', '' . $dbpassword . '');
        return $db;
    }
    

    With invalid data, your code cannot connect and therefore you’re receiving the connection-error.

    As they’re global to the dbc class, you’ll need to use $this when accessing them (such as $this->dbserver). Try updating your code to:

    function openDb() {
        $db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
        return $db;
    }
    

    * Also, though you may have removed it to post the question, you don’t have any of those variables set to actual values either.

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

Sidebar

Related Questions

I'm getting the following error in PHP : Fatal error: Uncaught exception 'PDOException' with
I'm getting this exception from facebook SDK: Fatal error: Uncaught OAuthException: (#506) Duplicate status
Fatal error: Uncaught exception 'ImagickDrawException' with message 'The given font is not found in
I'm getting this error when running this code: Fatal error: Uncaught exception 'DOMException' with
I am getting the following PHP Fatal Error Fatal error: Uncaught exception 'Exception' with
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template ./templates/diet-report.tpl on line
I'm getting the error fatal error C9999: *** exception during compilation *** Whenever I
Why am I getting the following error in my code? Fatal error: Can't use
New to android programming. Getting Following error. 04-07 14:49:05.452: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main java.lang.OutOfMemoryError
I'm getting this error when I'm running twitter autoreply bot (https://gist.github.com/820281) Fatal error: Uncaught

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.