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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:49:23+00:00 2026-05-27T22:49:23+00:00

I’m new to php oop.here i wanted to do database connectivity with singleton class

  • 0

I’m new to php oop.here i wanted to do database connectivity with singleton class but i got error like this:
Fatal error: Call to a member function getOne() on a non-object in C:\xampp\htdocs\singleton\new\singleton_db.php
here i have given two file

1.singleton_db.php

   <?php
        class database
        {
        public $query;
        public $results;
        public $conn;

             public static $database;

            //connect to the database
            public function __construct()
                {

                      $this->conn = mysql_connect('localhost','root','');
                      if ($this->conn)
            {
              mysql_select_db('test1');
            }

                }

            public static function instance() 
                {
                    if (!isset(self::$database)) {
                        self::$database = new database();
                    }

                    return self::$database;
                }
               function getOne($sql) {
             $result = $this->conn->getOne($sql); //Error in this line


             if(database::isError($result)) {
               throw new Exception($result->getMessage(), $result->getCode());
             }

             return $result;
           }

            function startTransaction() {
             //autoCommit returns true/false if the command succeeds
             return $this->conn->autoCommit(false);
           }

            function commit() {
             $result = $this->conn->commit();

             if(database::isError($result)) {
                 throw new Exception($result->getMessage(), $result->getCode());
             }

             $this->conn->autoCommit(true);
             return true;
           }

           function abort() {
             $result = $this->conn->rollback();

             if(database::isError($result)) {
               throw new Exception($result->getMessage(), $result->getCode());
             }

             return true;
           }



        //returns numerically indexed 1D array of values from the first column
         public function insert($table, $arFieldValues) {
             $fields = array_keys($arFieldValues);
             $values = array_values($arFieldValues);

             // Create a useful array of values
             // that will be imploded to be the
             // VALUES clause of the insert statement.
             // Run the mysql_real_escape_string function on those
             // values that are something other than numeric.
             $escVals = array();
             foreach($values as $val) {
               if(! is_numeric($val)) {
                 //make sure the values are properly escaped
                 $val = "'" . mysql_real_escape_string($val) . "'";
               }
               $escVals[] = $val;
             }
             //generate the SQL statement
             $sql = " INSERT INTO $table (";
             $sql .= join(', ', $fields);
             $sql .= ') VALUES(';
             $sql .= join(', ', $escVals);
             $sql .= ')';

             $hRes = mysql_query($sql);
             if(! is_resource($hRes)) {
               $err = mysql_error($this->conn) . "\n" . $sql;
               throw new Exception($err);
             }

             return mysql_affected_rows($hRes);
           }

        }

2.data.php

  <?php

               require_once('singleton_db.php');

               try {
                 $db = database::instance();
               } catch (Exception $e) {
                 // No point continuing...
                 die("Unable to connect to the database.");
               }

               $sql = "SELECT count(1) FROM mytable";
               $count = $db->getOne($sql);
               print "There are $count records in mytable!<br>\n";

               // start a transaction
               $db->startTransaction();

               // do an insert and an update
               try {
                 $arValues = array();
                 $arValues['id'] = '#id#';
                 $arValues['myval'] = 'blah blah blah';
                 $newID = $db->insert('mytable', $arValues);

                 print "The new record has the ID $newID<br>\n";

                 // update the record we just created
                 $arUpdate = array();
                 $arUpdate['myval'] = 'foobar baz!';
                 $affected = $db->update('mytable', $arUpdate, "id = $newID");

                 print "Updated $affected records<br>\n";

                 // write the changes to the database
                 $db->commit();
               } catch (Exception $e) {
                 // some sort of error happened - abort the transaction
                 // and print the error message
                 $db->abort();
                 print "An error occurred.<br>\n" . $e->getMessage();
               }

               ?>

What could I do to fix this ?

  • 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-05-27T22:49:24+00:00Added an answer on May 27, 2026 at 10:49 pm

    Your problem is you haven’t defined the

    getOne(); 
    

    method properly. The property

    $this->conn
    

    Is just the result of mysql_connect() function which is a “MySQL link identifier on success, or FALSE on failure”. It is not an object, and such, you can not ask it for the getOne(); method.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported

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.