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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:02:47+00:00 2026-06-14T03:02:47+00:00

i am trying to use PDO in php using classes. so i have defined

  • 0

i am trying to use PDO in php using classes.

so i have defined one config file like that

<?php
global $configVars;

    $configVars['online'] = false;  

    if(($_SERVER['SERVER_NAME'])!='localhost' and ($_SERVER['SERVER_NAME'])!='abc')
    {
$configVars['dbhost']       = "localhost";      
$configVars['dbuser']       = "dbuser";   
$configVars['dbpassword']   = "dbpass";     
$configVars['dbname']       = "dbname"; 

}
?>

then i have defined class to connect the database

<?php
class dbClass{
var $dbHost,
      $dbUser,
        $dbName,
          $dbPass,
            $dbTable,
              $dbLink,
                $resResult,
                  $dbh,
                  $dbPort;  


        function dbClass(){
            global $configVars;
            $this->dbHost = $configVars['dbhost'];
            $this->dbUser = $configVars['dbuser'];
            $this->dbPass = $configVars['dbpassword'];
            $this->dbName = $configVars['dbname'];
            $this->connect_PDO();
        }

        function connect_PDO(){
            try {
            $dbh = new PDO('mysql:host='.$this->dbHost.';dbname='.$this->dbName.'', $this->dbUser, $this->dbPass);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected.";
            }
            catch(PDOException $e) {
            echo "I'm sorry Charlie, I'm afraid i cant do that.";
            file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
            $dbh = null;
            }
        }
 }       

?>

now i have defined another class to select the records from table

<?
class cms extends dbClass
{

function get_rec($id=0)
    {
        ($id==0?$addQuery="":$addQuery=" where id =".$id);
        $statement = $dbh->prepare("select * from TABLENAME :name order by id");
        $statement->execute(array(':name' => $addQuery));
        $row = $statement->fetchAll(); 
        return $row ;
    }   


}
?>

now when i am using this function in my PHP file like this

<?php 
$objCMS=new cms();
$getCMS=$objCMS->get_rec(1);
echo $getCMS[0];
?>

i got following out put

Connected.
Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs

i am trying to sort this out from last 2 days but no success.

i am new to PDO and want to use it.

Thanks

  • 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-14T03:02:49+00:00Added an answer on June 14, 2026 at 3:02 am

    You need to call dbClass() in your constructor for cms

    add this to the cms class

    function cms() {
        parent::dbClass();
    }
    

    and change get_rec to have:

    $statement = $this->dbh->prepare("select * from TABLENAME :name order by id");
    

    when using $dbh you need to reference it as $this->dbh

    You need to make the $dbh to $this->dbh changes in you need to update connect_PDO() also

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

Sidebar

Related Questions

I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying to port a php file that is used with MySQL. My
I am trying to use PDO::FETCH_CLASS on an object. I am using namespacing and
Good morning all, I'm trying to use a smarty MySql one-line that will do
I'm trying to use prepared statements in a PHP script that accesses an SQLite3
Hi I am using a PHP Symfony project and am trying to use the
Trying to make insert or update sql using the prepared statements from php's pdo.
I've been trying to use SQLite with the PDO wrapper in PHP with mixed
I'm trying to use AJAX to send values to PHP file, which then updates
I'm trying to use PDO in this way and reuse the output template: selected_products_show

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.