SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fatal error: Call to a member function prepare() on a non-object

    Hello , i get this error message: Fatal error: Call to a member function prepare() on a non-object in database_handler.php on line 62.

    its from the book beginning PHP and MySQL E-Commerce Second edition.

    Code:
    <?php
    class DatabaseHandler
    {
    private static $_mHandler;

    private function __construct()
    {
    }

    private static function GetHandler()
    {
    //skapa en db connection om det inte redan finns en
    if (isset(self::$_mHandler))
    {
    try
    {
    self::$_mHandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => DB_PERSISTENCY));
    self::$_mHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch (PDOException $e)
    {
    //stäng databashanteringen och trigga error
    self::Close();
    trigger_error($e->getMessage(), E_USER_ERROR);
    }
    }
    return self::$_mHandler;
    }

    public static function Close()
    {
    self::$_mHandler = null;
    }

    public static function Execute($sqlQuery, $params = null)
    {
    try
    {
    // hämta databas
    $database_handler = self::GetHandler();
    // förbered query för execution
    $statement_handler = $database_handler->prepare($sqlQuery);
    // kör queryn
    $statement_handler->execute($params);
    }

    catch(PDOException $e)
    {
    self::Close();
    trigger_error($e->getMessage(), E_USER_ERROR);
    }
    }

    public static function GetAll($sqlQuery, $params = null, $fetchStyle = PDO::FETCH_ASSOC)
    {
    $result = null;
    try
    {
    $database_handler = self::GetHandler();
    line 62: $statement_handler = $database_handler->prepare($sqlQuery);
    // kör queryn
    $statement_handler->execute($params);
    // hämta resultat
    $result = $statement_handler->fetchAll($fetchStyle);
    }

    catch(PDOException $e)
    {
    self::Close();
    trigger_error($e->getMessage(), E_USER_ERROR);
    }
    return $result;
    }

  2. #2
    SitePoint Addict
    Join Date
    Oct 2006
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following statement has an error:
    if (isset(self::$_mHandler))

    Replace with: (notice the '!')
    if (!isset(self::$_mHandler))

    mikem

  3. #3
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx alot for the help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •