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 ?

tereško
  • 58,060
  • 25
  • 98
  • 150
nic
  • 929
  • 3
  • 14
  • 22
  • 3
    Why are you implementing your own database class, when PHP already provides perfectly good ones in the shape of MySQLI and PDO? – GordonM Dec 30 '11 at 11:38

1 Answers1

1

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.

Slavic
  • 1,891
  • 2
  • 16
  • 27