PHP:Can';t访问SQL语句中的类


PHP: Can't access class in SQL Statement

我在使用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"这样的数字时,它会按预期工作。

有人知道我做错了什么吗?

非常感谢!:-)

第1版:完整代码:

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

$dozent->id未在function 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));

尝试使用"…{$dozent->id};"。用双引号和括号。