2014-11-02 1 views
1

다른 유사한 링크를 사용하여 내 질문에 답을 얻으려고했지만 couldn 해결 안해. 다른 질문은 PHP 클래스가 있다고 인정하고이를 사용합니다. 필자의 경우 이미 PHP 클래스가 있으며, 여전히 동일한 오류가 발생합니다. 내가 사용 culttt.com/2012/10/01/roll-your-own-pdo-php-class/PHP 치명적 오류 : admin includes html database.class.php에있는 객체가 아닌 객체에서 prepare() 함수를 호출하십시오.

내 경우

:

나는 다음 링크의 설명에 따라이 클래스를 만든 MS SQL.

첫 번째 시도 :

는 다음 아파치 서버에 다음과 같은 결과를 반영 다른 파일 (phppdo.php)에서 다음 PHP 코드를 실행 database.class.php

class Database{ 
private $dbtype = DB_TYPE; 
private $host  = DB_HOST; 
private $user  = DB_USER; 
private $pass  = DB_PASS; 
private $dbname = DB_NAME; 

private $dbh; 
private $error; 

private $stmt; 

public function __construct(){ 
    // Set DSN 
    if ($this->dbtype == 'mysql') { 
     $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; 
    } 
    if ($this->dbtype == 'sqlsrv') { 
     $dsn = 'sqlsrv:server=' . $this->host . ';Database=' . $this->dbname; 
    } 

    // Set options 
    $options = array(
     PDO::ATTR_PERSISTENT => true, 
     PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
    ); 
    // Create a new PDO instanace 
    try{ 
     $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); 
    } 
    // Catch any errors 
    catch(PDOException $e){ 
     $this->error = $e->getMessage(); 
    } 
} 

public function query($query){ 
$this->stmt = $this->dbh->prepare($query); 
} 

public function bind($param, $value, $type = null){ 
if (is_null($type)) { 
    switch (true) { 
     case is_int($value): 
      $type = PDO::PARAM_INT; 
      break; 
     case is_bool($value): 
      $type = PDO::PARAM_BOOL; 
      break; 
     case is_null($value): 
      $type = PDO::PARAM_NULL; 
      break; 
     default: 
      $type = PDO::PARAM_STR; 
    } 
} 
$this->stmt->bindValue($param, $value, $type); 
} 

public function execute(){ 
return $this->stmt->execute(); 
} 

public function resultset(){ 
$this->execute(); 
return $this->stmt->fetchAll(PDO::FETCH_ASSOC); 
} 

public function single(){ 
$this->execute(); 
return $this->stmt->fetch(PDO::FETCH_ASSOC); 
} 

public function rowCount(){ 
return $this->stmt->rowCount(); 
} 

public function lastInsertId(){ 
return $this->dbh->lastInsertId(); 
} 

public function beginTransaction(){ 
return $this->dbh->beginTransaction(); 
} 

public function endTransaction(){ 
return $this->dbh->commit(); 
} 

public function cancelTransaction(){ 
return $this->dbh->rollBack(); 
} 

public function debugDumpParams(){ 
return $this->stmt->debugDumpParams(); 
} 
} 

의 코드 phppdo.php를 실행하면 결과가 반영됩니다.

phppdo.php을 실행함으로써 다음과 같은 오차 반영

번째 시도 :

가 은 치명적인 오류가

: C의 비 객체() 제조 멤버 함수 호출 : \ XAMPP \ htdocs를 \ sattest \ 관리 \ 포함을 \ html \ database.class.php on line 45

phppdo.php를 세 번째로 실행하면 Apache 서버가 충돌하고 Apache 서비스가 다시 시작됩니다.

네 번째 시도는 첫 번째 시도만큼 결과를 잘 보여줍니다. 테스트를 위해이 파일을 반복해서 실행하면이주기가 반복됩니다.

[Mon Nov 03 09:35:27.883145 2014] [core:warn] [pid 14276:tid 256] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run? 
[Mon Nov 03 09:35:28.796237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00455: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 configured -- resuming normal operations 
[Mon Nov 03 09:35:28.796237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00456: Server built: Aug 18 2012 12:41:37 
[Mon Nov 03 09:35:28.796237 2014] [core:notice] [pid 14276:tid 256] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache' 
[Mon Nov 03 09:35:28.799237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00418: Parent: Created child process 4528 
[Mon Nov 03 09:35:30.490406 2014] [mpm_winnt:notice] [pid 4528:tid 268] AH00354: Child: Starting 150 worker threads. 
[Mon Nov 03 09:35:36.041961 2014] [:error] [pid 4528:tid 1756] [client 127.0.0.1:63411] script 'C:/xampp/htdocs/sattest/nzta-forms/admin/pdodb-tutorial.php' not found or unable to stat 
[Mon Nov 03 09:36:40.478404 2014] [:error] [pid 4528:tid 1716] [client 127.0.0.1:63418] PHP Fatal error: Call to a member function prepare() on a non-object in C:\\xampp\\htdocs\\sattest\\nzta-forms\\admin\\includes\\html\\database.class.php on line 45 
[Mon Nov 03 09:36:44.453802 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00428: Parent: child process exited with status 3221225477 -- Restarting. 
[Mon Nov 03 09:36:45.002856 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00455: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 configured -- resuming normal operations 
[Mon Nov 03 09:36:45.002856 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00456: Server built: Aug 18 2012 12:41:37 
[Mon Nov 03 09:36:45.002856 2014] [core:notice] [pid 14276:tid 256] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache' 
[Mon Nov 03 09:36:45.004857 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00418: Parent: Created child process 14456 
[Mon Nov 03 09:36:46.597016 2014] [mpm_winnt:notice] [pid 14456:tid 268] AH00354: Child: Starting 150 worker threads. 

도와 주셔서 감사합니다 사전에 t : 다음

// Include database class 
include('database.class.php'); 

// Define configuration 
define("DB_TYPE", "sqlsrv"); // Values allowed: sqlsrv, mysql, sqlite 
define("DB_HOST", "localhost"); 
define("DB_USER", "dbuser"); 
define("DB_PASS", "password"); 
define("DB_NAME", "dbname"); 

$database = new Database(); 


$database->query('SELECT * FROM incidents WHERE incident_id = 1 '); 

$database->execute(); 

$rows = $database->resultset(); 

echo "<pre>"; 
print_r($rows); 
echo "</pre>"; 

아파치 오류 로그입니다. 나는 이것을 가능한 빨리 해결할 필요가있다. 튜토리얼은 바보

그건 ...

~ Satinder

+0

메이트, 그 튜토리얼은 어리석은 ... 왜 PDO를 장식하고 새로운 객체의 기능/기능을 제거하겠습니까? 그리고 생성자는 예외를 먹고 있습니다. 그것은 $ this-> error (사적인)에 기록하지만 변수 – Tivie

답변

2

글쎄, 내가 의견을 거라고하지만 너무 큰 얻었다. 먼저 PDO 클래스를 랩핑 (장식)하지만 클래스의 대부분의 기능을 제거합니다. 또한 생성자에서 예외가 발견되고 액세스 할 수있는 방법이없는 개인 변수에 메시지가 저장됩니다. 디버깅이 제대로되지 않는다는 의미입니다. 여하튼

...

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\sattest\admin\includes\html\database.class.php on line 45

$dbh 개체없는 것을 의미한다. $dbh은 PDO 인스턴스이므로 해당 개체를 만드는 동안 문제가 발생합니다 (스크립트가 데이터베이스에 연결할 수 없습니다. 누가 예외인지 알 수 없으므로 예외가 발생합니다).

그래서 다음을 수행하십시오. database.class에서.

// Create a new PDO instanace 
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options); 

을하고 당신이 얻을 예외를 게시 :이와

// Create a new PDO instanace 
try{ 
    $this->dbh = new PDO($dsn, $this->user, $this->pass, $options); 
} 
// Catch any errors 
catch(PDOException $e){ 
    $this->error = $e->getMessage(); 
} 

을 : PHP는 생성자이를 교체합니다. 예외 상황은 일어나는 일을 스스로 설명해야합니다.

+0

에 액세스 할 수있는 방법이 없습니다. Tivie에게 도움을 주셔서 감사합니다. – satinder

+0

나는 그 데이터베이스 클래스를 버리고 php 파일에서 pdo 코드를 직접 사용했다. 잘 작동합니다. 오류없이. 어쨌든, 저는 PHP와 PDO에 대해 아직 새로운 것입니다. 당신은 나에게 '어떻게하는지'또는 PDO의 수업이나 기능을 만드는 방법을 찾을 수있는 곳을 가르쳐 주시겠습니까? 쿼리를 보내고 변수에 저장된 결과를 얻고 싶습니다. 그에 따라 처리 할 수 ​​있습니다. 다시 한번 고마워! – satinder

관련 문제