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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:16:11+00:00 2026-06-16T01:16:11+00:00

This is my code: class DB { static $instance; function get() { if (self::$instance

  • 0

This is my code:

class DB {
static $instance;

function get() {
    if (self::$instance === null) {
        self::$instance = new PDO('mysql:host=localhost;dbname=forum', 'root', 'root');
    }
    return self::$instance;
}

function getAllUsers() {
    $users = array();
    $sql = "SELECT * FROM users";
    foreach (self::get()->query($sql) as $row) {
        $users[] = new User($row);
    }
    return $users;    
}
}

And now I’m calling the getAllUsers function in here:

class App {
function showUsers() {
    $users = DB::getAllUsers();
}

Except its giving me an error:

Warning: Invalid argument supplied for foreach()

When I’m doing var_dump on self::get im getting a bool(false)

Can someone tell me what I’m doing wrong?

  • 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-16T01:16:12+00:00Added an answer on June 16, 2026 at 1:16 am

    PDO::query returns a PDOStatement object only if it is successful in sending the query to the database and getting back a valid response (i.e. there were no SQL errors returned by the driver). However, in the event that it fails it will return false. So you must check for this in order to handle errors. Also, you can optionally put PDO into Exception Mode to have it throw an exception any time it encounters an error. That way you can use try/catch to handle errors whenever call on PDO objects. See the example below…

    With PDO’s default error mode (silent):

    if ($result = self::get()->query($sql)) {
        /* Iterate here */
        foreach ($result as $row) {
            /* Do whatever you want with $row */
        }
    } else {
        /* Handle errors here */
        echo "There was an error with the query!";
        var_dump(self::get()->errorInfo());
    }
    

    With PDO in Exception Mode:

    /* Put PDO Into Exception Mode */
    self::get()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        foreach (self::get()->query($sql) as $row) {
            /* Do whatever you want with $row */
        }
    } catch(PDOException $e) {
        /* If PDO fails we handle it here */
        echo "Your query failed: " . $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code running on PHP 5.2.6 class Singleton { static private $instance
First look at this code: class Program { static void Main(string[] args) { var
Take a look at this code: public class Test { public static void main(String...
please consider this code : 1)public static class MyClass 2){ 3) public static DateTime
Okay, so i have this code i wrote: class Connection { public static StreamWriter
With this code: public partial class Form1 : Form { private static readonly int
Try this piece of code - public class WhitespaceTest { public static void main(String[]
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
I have a piece of code like this: Class Program { static StreamReader sr
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]

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.