2011-12-30 2 views
0

php oop.here에 처음입니다. 싱글 톤 클래스로 데이터베이스 연결을 원했지만 다음과 같은 오류가 발생했습니다. 치명적인 오류 : C의 비 객체의 멤버 함수 getOne는() : \ XAMPP \ htdocs를 \ 싱글 여기 새로운 \의 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(); 
       } 

       ?> 

문제를 해결하려면 어떻게해야합니까?

+3

PHP가 MySQLI 및 PDO의 형태로 완벽하게 좋은 데이터베이스 클래스를 제공 할 때 왜 자신의 데이터베이스 클래스를 구현합니까? – GordonM

답변

1

귀하의 문제는 당신이 제대로

getOne(); 

방법을 정의하지 않은 것입니다. 속성

$this->conn 

는 "MySQL의 링크 식별자 성공 또는 실패 FALSE"인로 mysql_connect() 함수의 단지 결과이다. 객체가 아니기 때문에 getOne()을 요청할 수 없습니다. 방법.

관련 문제