2016-04-02 11 views
-3

SQL文にいくつか問題があります。PHP:SQL文のクラスにアクセスできない

require_once("Dozent.php"); 
. 
. 
. 

public function findAll() 
    { 
     try { 
      $stmt = $this->pdo->prepare(' 
       SELECT * FROM vorlesung WHERE id_dozent = $dozent->id; 
      '); 
      $stmt->execute(); 
      $stmt->setFetchMode(PDO::FETCH_CLASS, 'Vorlesung'); 
      return $stmt->fetchAll(); 

     } catch (PDOException $e) { 
      echo("Fehler! Bitten wenden Sie sich an den Administrator...<br>" . $e->getMessage() . "<br>"); 
      die(); 
     } 
    } 

'id_dozent = $ dozent-> id'は機能しません。理由はわかりません。 '1'のような数字を挿入すると、期待どおりに動作します。

誰かが私が間違っていることを知っていますか?

ありがとうございます! :-)

EDIT1:フルコード: "... {$ dozent->のid};"

<?php 

require_once("Manager.php"); 
require_once("Vorlesung.php"); 

require_once("Dozent.php"); 
require_once("DozentManager.php"); 

class VorlesungManager extends Manager 
{ 
    protected $pdo; 

    public function __construct($connection = null) 
    { 
     parent::__construct($connection); 
    } 

    public function __destruct() 
    { 
     parent::__destruct(); 
    } 

    public function findAll() 
    { 
     try { 
      $stmt = $this->pdo->prepare(' 
       SELECT * FROM vorlesung WHERE "id_dozent = $dozent->id"; 
      '); 
      $stmt->execute(); 
      $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Vorlesung'); 
      return $stmt->fetchAll(); 
     } catch (PDOException $e) { 
      echo("Fehler! Bitten wenden Sie sich an den Administrator...<br>" . $e->getMessage() . "<br>"); 
      die(); 
     } 

    } 
+0

http://php.net/manual/en/pdo.error-handling.php - http://php.net/manual/en/function.error-reporting.php –

+0

ここで、 '$ dozent'と'$ dozent-> id'が定義されていますか? – VolkerK

+0

1)一重引用符は*補間されません2)正しいコードを書いてください(プレースホルダ付き)(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq = 1)(この問題を完全に回避する) – user2864740

答えて

0

$dozent->idfunction findAll()

内で定義されていないあなたは、引数を使用する必要があります:

public function findAll($dozent) 

をし、クエリでその値を含めるために、より正しい方法は次のようなものです:

$stmt = $this->pdo->prepare('SELECT * FROM vorlesung WHERE id_dozent = ?'); 
$stmt->execute(array($dozent->id)); 

または

$stmt = $this->pdo->prepare('SELECT * FROM vorlesung WHERE id_dozent = :id'); 
$stmt->execute(array(':id' => $dozent->id)); 
+0

ありがとう@CJニーム! それは今動作しています! –

+0

喜んで助けてください! :) –

-1

をしてみてください。二重引用符と括弧で囲みます。

関連する問題