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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:53:19+00:00 2026-05-11T13:53:19+00:00

Trying out PDO for the first time. $dbh = new PDO(mysql:host=$hostname;dbname=animals, $username, $password); $stmt

  • 0

Trying out PDO for the first time.

$dbh = new PDO('mysql:host=$hostname;dbname=animals', $username, $password);  $stmt = $dbh->query('SELECT * FROM animals'); $stmt->setFetchMode(PDO::FETCH_INTO, new animals);  foreach($stmt as $animals) {     echo $animals->name; } 

If i skip the setFetchMode() method, then I need to call $animals['name'] which I do not want.

But I do not want to call the setFetchMode() for each query I do.

Is there a way to set the default FetchMode ? Or some other method to make the query() return objects with one global setting.

  • 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. 2026-05-11T13:53:19+00:00Added an answer on May 11, 2026 at 1:53 pm

    Perhaps you could try extending the PDO class to automatically call the function for you… in brief:

     class myPDO extends PDO {    function animalQuery($sql)    {      $result = parent::query($sql);      $result->setFetchMode(PDO::FETCH_INTO, new animals);      return $result;    }  //   // useful if you have different classes //   function vegetableQuery($sql) //   { //     $result = parent::query($sql); //     $result->setFetchMode(PDO::FETCH_INTO, new vegetables); //     return $result; //   } }  $dbh = new myPDO('mysql:host=$hostname;dbname=animals', $username, $password);  $stmt = $dbh->animalQuery('SELECT * FROM animals');  foreach($stmt as $animals) {     echo $animals->name; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying out PDO in PHP for the first time. I thought that
Trying to move to PDO can't figure out what is wrong with this $dbh
Trying out databinding for the first time, but something isn't working. I'm trying to
Am trying out structuremap for the first time and am getting the following compiler
Trying out a problem of finding the first k digits of a num^num I
I have been trying to figure out the best way to include my PDO
I'm new to the Zend Framework and trying to figure some stuff out. I
Im trying out the new additions of Socket-communication (TCP & UDP) in the Beta
Hello i was trying to make function with PDO but getting error (new to
I am trying to insert a serialize data into mySQL using PDO and I'm

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.