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.