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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:59:49+00:00 2026-06-16T18:59:49+00:00

I am trying to fetch multiple rows into instances while passing an argument to

  • 0

I am trying to fetch multiple rows into instances while passing an argument to the constructor(the PDO connection). I could fetch one row at a time and then construct them but I am looking for a one line solution.

class Member {

        // PDO connection
        public $conn;

        public $Members_ID;
        public $Members_FirstName;
        public $Members_LastName;


        function __construct($conn){         
            $this->conn= $conn;
        }


        // Returns an array of Members with the email address that is given
        // (Multiple members can have the same email)
        public static function getMembersFromEmailAddress($emailAddress, $conn){

            try 
            {
                $result = $conn->query("SELECT * FROM members WHERE Members_Email ='". $emailAddress ."'"); 

               //HERE I WANT MEMBERS TO BE AN ARRAY OF MEMBER OBJECTS
                $members = $result->fetch(PDO::FETCH_INTO, new Member($conn));
                return $members
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();
            }

}
  • 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-06-16T18:59:50+00:00Added an answer on June 16, 2026 at 6:59 pm

    What about:

    $args= array();
    $args[] = $conn;
    
    $members = $result->fetchAll(PDO::FETCH_CLASS, 'Member', $args);
    

    FETCH_INTO is used for adding data into an already existing class, so in your case using FETCH_CLASS makes more sense.


    Side note: Since you are already using PDO, you might as well take advantage of prepared statements. This provides the benefit of not having to worry about SQL injections.

    The only thing in your code that would change is:

    $stmt = $conn->prepare("SELECT * FROM members WHERE Members_Email = :member_email");
    $result = $stmt->execute(array(":member_email" => $emailAddress));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 10.0 and 10.5 I'm trying to fetch multiple rows into one
I am trying to get multiple rows to insert into a mysql table from
I am trying to write a SQL query to fetch rows whose fields contain
I am trying to update multiple rows here.However I fail to point the right
I am trying to fetch data from multiple tables depending on what is selected
Trying to get into Rails and feedzirra! :) I have stores multiple feed urls
i was trying to insert a string into database having multiple value using componentsSeparatedByString:@,.
I'm trying to create a stored function in oracle that returns multiple rows. My
i am trying to push multiple arrays into 1 big array, resulting in a
Trying to get the results of one query into the WHERE statement of another:

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.