-3

I have some problems with a SQL statement.

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' that's not working and I don't know why. When I insert a number like '1' it's working as expected.

Does anybody know what I am doing wrong?

Thanks so much! :-)

Edit1: Full Code:

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

    }

2 Answers2

0

$dozent->id isn't defined inside function findAll()

You have to use an argument:

public function findAll($dozent)

And a more correct way to include that value in the query is something like this:

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

Or this:

$stmt = $this->pdo->prepare('SELECT * FROM vorlesung WHERE id_dozent = :id');
$stmt->execute(array(':id' => $dozent->id));
CJ Nimes
  • 648
  • 7
  • 10
-1

Try with "... {$dozent->id};". With the double quotes and brackets.

NathanVss
  • 634
  • 5
  • 16