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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:07:56+00:00 2026-06-17T13:07:56+00:00

I am having some trouble to get database query using below code. I am

  • 0

I am having some trouble to get database query using below code. I am first time using classes and even PDO for connection so no idea what is wrong. For you knowledge I am not so master but trying to learn class so I would appreciate if you can tell me if anything can improve for this too.

Error Message: Fatal error: Call to a member function query() on a non-object in C:\ pat \ to\ class.php on line 72

<?php

    // get database connection
    private static function _db_connect() {

        try {

            $db_con = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
            $db_con = null;

        } catch (PDOException $e) {

            print "Error!" . $e->getMessage(). "<br/>";

        }

    }


    // get database tables prefix
    public static function prefix() {

        $prefix = 'tb_'; // once done will be set dynamically

        return $prefix;
    }


    // get all users from db
    public static function get_users() {

        $db = self::_db_connect();
        $prf = self::prefix();

        $st = $db->query('SELECT * FROM '.$prf.'users'); // this is the line #72 where error

        while($row = $st->fetch(PDO::FETCH_ASSOC))
         $rs[] = $row;

        return count($rs) ? $rs : array();

    }    


?>

EDIT: I have remove null and return PDO object here

    // get database connection
    private static function _db_connect() {

        try {

            $db_con = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);
            return $db_con;

        } catch (PDOException $e) {

            print "Error!" . $e->getMessage(). "<br/>";

        }

    }


    // get database tables prefix
    public static function prefix() {

        $prefix = 'tb_'; // once done will be set dynamically

        return $prefix;
    }


    // get all users from db
    public static function get_users() {

        $db = self::_db_connect();
        $prf = self::prefix();

        $st = $db->query('SELECT * FROM '.$prf.'users'); // this is the line #72 where error

        while($row = $st->fetch(PDO::FETCH_ASSOC))
         $rs[] = $row;

        return count($rs) ? $rs : array();

    }    


?>
  • 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-17T13:07:57+00:00Added an answer on June 17, 2026 at 1:07 pm

    You’re not returning anything from your db_connect method, so it returns NULL by default. You’re trying to call, in essence, NULL->query(), which obviously doesn’t make sense.

    Modify db_connect to return the PDO object it creates.

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

Sidebar

Related Questions

I'm having some trouble using regular expression to get date in a string. Example
Im having some trouble with an oracle database. Every time i try to connect,
I'm having some trouble integrating two pieces of code. The first checks the size
I'm having some trouble getting a search by zip code proximity query working. I've
I'm having some trouble pulling up relationed objects from my database using Doctrine2 in
I'm having some trouble with my EF Code First model when saving a relation
I am using CodeCanyon's Price Calculator, and am having some Javascript trouble. The first
Basically having some trouble with using Hover to hide or show an item. The
I am extremely new to Informix and am having some trouble trying to get
I have the following 3 tables in my database, and am having some trouble

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.