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

  • Home
  • SEARCH
  • 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 6637385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:18:30+00:00 2026-05-25T23:18:30+00:00

Using PDO::setAttribute , how do I provide the class name when setting PDO::ATTR_DEFAULT_FETCH_MODE to

  • 0

Using PDO::setAttribute, how do I provide the class name when setting PDO::ATTR_DEFAULT_FETCH_MODE to PDO::FETCH_CLASS.

This is the code I am using.. I would like to set it so all of my rows are returned as an instance of DB_Row:

class DB_Row extends ArrayObject {}

$db = new PDO('mysql:dbname=example;host=localhost', 'user', 'pass');
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);

$stmt = $db->query("SELECT * FROM `table` WHERE `id` = 1;");

$row = $stmt->fetch(); // I want a DB_Row by default!

The code above results in a PDOException since the DB_Row class name was not assigned.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General    error: No fetch class specified

How would I go about this?

Thanks in advance..

SOLUTION: I used fireeyedboy‘s answer. It worked the best for my situation as I was already extending PDOStatement for logging purposes…

class DB extends PDO {
    public function __construct($host = null, $user = null, $pass = null, $db = null) {
        try {
            parent::__construct('mysql:dbname=' . $name .';host=' . $host, $user, $pass);
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DB_Query', array('DB_Row')));
        } catch (PDOException $e) {
            die('Database Error');
        }
    }
}

class DB_Query extends PDOStatement {
    private $class;
    protected function __construct ($class = 'DB_Row') {
        $this->class = $class;
        $this->setFetchMode(PDO::FETCH_CLASS, $this->class);
    }
}

class DB_Row extends ArrayObject {
    public function __set($name, $val) {
        $this[$name] = $val;
    }
    public function __get($name) {
        return $this[$name];
    }
}
  • 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-05-25T23:18:30+00:00Added an answer on May 25, 2026 at 11:18 pm

    Another kind of hack would be to extend PDOStatement, override its fetch methods and let your PDO instance use that as the default statement class.

    As an example I’ll just demonstrate overriding fetch()1 and leave fetchAll() and what have you up to you, if you wanna go this route:

    class Db_Row
    {
    }
    
    class PDOStatementWithClass
        extends PDOStatement
    {
        private $fetch_class;
    
        // PHP complained when I tried to make this public
        protected function __construct( $fetch_class = 'StdClass' )
        {
            // internally set the fetch class for later use
            $this->fetch_class = $fetch_class;
        }
    
        // let $fetch_style default to PDO::FETCH_CLASS in stead of PDO::FETCH_BOTH
        public function fetch( $fetch_style = PDO::FETCH_CLASS, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0 )
        {
            // make sure we're really dealing with the correct fetch style
            if( $fetch_style == PDO::FETCH_CLASS )
            {
                // then automatically set the fetch mode of this statement
                parent::setFetchMode( $fetch_style, $this->fetch_class );
            }
    
            // go ahead and fetch, we should be good now
            return parent::fetch( $fetch_style, $cursor_orientation, $cursor_offset );
        }
    }
    
    $db = new PDO( /* etc... */ );
    // set default fetch mode to FETCH_CLASS
    $db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS );
    // override what statement class to use, and provide constructor arguments (found out by trial and error)
    $db->setAttribute( PDO::ATTR_STATEMENT_CLASS, array( 'PDOStatementWithClass', array( 'Db_Row' ) ) );
    

    This has the added benefit that you’ll only have to define your PDO::FETCH_CLASS once in your application, and not in every query.


    1) I’m surprised PHP didn’t complain about overriding the signature of the method by the way.

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

Sidebar

Related Questions

Using this php code: try{ $dbh = new PDO(mysql:host=$host;dbname=$dbname,$user,$pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //
I would like to trigger a popup box using the function provide by jquery
I would like to know what query is executed using PHP PDO. I have:
Iam having some trouble with a PDO execute statement. My code looks like this:
I'm using PDO bindValue to search terms through my database. Is my syntax all
I'm having some problems while using PDO. It all seems correct but I am
Can someone confirm this: If I am using PDO::PARAM, I don't need to Filter
I am using MySQL and PHP 5.3 and tried this code. $dbhost = 'localhost';
Can i join warning as well as error for pdo's using set attribute right
I'm using PDO's fetch_class feature to retrieve data from my database row into object,

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.