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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:15:14+00:00 2026-06-09T22:15:14+00:00

So I’m trying to build a simple PDO connection class so I can create

  • 0

So I’m trying to build a simple PDO connection class so I can create a DB object whenever I need to have DB access but I keep getting the same errors no matter what I try.

Here is my code for the DB class

<?php
class DbConnect
{
// Database login vars
private $dbHostname = 'localhost';
private $dbDatabase = 'database';
private $dbUsername = 'username';
private $dbPassword = 'password';
public $db = null;

public function connect()
{
    try
    {
        $db = new PDO("mysql:host=".$this->dbHostname.";dbname=".$this->dbDatabase, $this->dbUsername, $this->dbPassword); // Establish DB connection
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set error handling
        return $db;
    }
    catch(PDOException $e)
    {
        echo "It seems there was an error.  Please refresh your browser and try again. ".$e->getMessage();
    }
}
}
?>

And here is my code for the class I’m trying to use it in:

<?php
session_start();
require 'DbConnect.php';

class User
{
    public function bindParams()
    {
        $DbConnect = new DbConnect();
        $sql = $DbConnect->connect();

        $sql->prepare("insert into dbobjectex(firstName, lastName) values (:firstName, :lastName)");
        $sql->bindParam(':firstName', $_SESSION['firstName']);
        $sql->bindParam(':lastName', $_SESSION['lastName']);
        $sql->execute();
    }
}
?>

Then bindParams() is called from the spot where I create the User class. The two errors I keep getting are “NetworkError: 500 Internal Server Error” and “The character encoding of the HTML document was not declared.” Any ideas on what I’m doing wrong here? Thanks for any advice.

  • 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-09T22:15:15+00:00Added an answer on June 9, 2026 at 10:15 pm

    What am I doing wrong here? – Many things, but the most obvious one to miss is this: at the end of your class definition of DbConnect, you’re closing the php tag ?>, which you must avoid as much as possible with included files with included files. The upshot of having them in your case is that headers are sent prematurely, explaining your second error.

    Other then that, each time you’re instantiating the user class, you’re creating a new PDO instance: you assign it to a variable, local to the connect member function, at least replace $db with $this->db. But most of all, I agree with @sabre: why would PDO require an additional wrapper? especially one that does nothing more than instantiate and return a pdo instance, just create an abstract class that handles the connections if needs must.

    Another thing you might want to check is weather or not the include path is correct, and read the docs on autoloading classes. It’ll save you a world of trouble once you’ve got a substantial class library. This might explain your first error, as might the ?> tag, but just to be on the safe side: use require_once, rather than require: including, and thus redefining, a class a second time is not what you want to be doing at all.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put

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.