致命错误:在非对象上调用成员函数getOne()

时间:2011-12-30 11:33:01

标签: php

我是php oop的新手。我想用singleton类做数据库连接,但我得到这样的错误: 致命错误:在C:\ xampp \ htdocs \ singleton \ new \ singleton_db.php中的非对象上调用成员函数getOne() 这里我给了两个文件

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();
               }

               ?>

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的问题是您尚未定义

getOne(); 

方法正确。物业

$this->conn

只是mysql_connect()函数的结果,它是“成功时的MySQL链接标识符,或失败时为FALSE”。它不是一个对象,因此,你不能要求getOne();方法