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.

Matt Whitehead
  • 1,743
  • 3
  • 19
  • 34
  • Have you checked your Apache error log? – DCoder Aug 13 '12 at 09:13
  • 3
    This doesnt sound to me like a problem with your PDO class. Its usually problems with server configuration that cause Internal Server Errors. Check your apache error logs, you'll get a more detailed error message there. – martynthewolf Aug 13 '12 at 09:14
  • Take a look at your server logs, for more detailed error message – Aurimas Ličkus Aug 13 '12 at 09:15
  • I have to ask, why create a connection class at all? your restricting the User object by that if you have more then one method then you could possibly be creating multiple instances of the DbConnect class. – Lawrence Cherone Aug 13 '12 at 09:18
  • Why are you using emulated prepares ? – tereško Aug 13 '12 at 09:21
  • I checked the error log and there aren't any errors from around the last time the error showed up in Firebug. I'm building it so as to keep from repeating the same code over and over again whenever I need to access the DB. @tereško Sorry but what do you mean by emulated prepares? – Matt Whitehead Aug 13 '12 at 09:28
  • Also, I've already tried restarting Apache a couple times too. – Matt Whitehead Aug 13 '12 at 09:28
  • 2
    @Freethinker I don't understand the point of this class. Why does PDO, a well defined and rigid OO interface, need a wrapper class? Are you just doing this so you don't have to pass the connection parameters and set the ERRMODE every time? – DaveRandom Aug 13 '12 at 09:32
  • @DaveRandom Yes, isn't that the purpose of the DRY method and one of the great things about OOP is being able to create re-useable objects? – Matt Whitehead Aug 13 '12 at 09:35
  • @Freethinker Consider doing something like [this](http://codepad.org/71o5UbZJ) instead then. FTR, I think hard-coding usernames and passwords into classes is a bad idea because it *reduces* re-usability. – DaveRandom Aug 13 '12 at 09:39

1 Answers1

1

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.

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149