我在使用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-&gt; id'不起作用,我不知道为什么。当我插入一个像'1'这样的数字时,它按预期工作。
有人知道我做错了吗?
非常感谢! : - )
Edit1:完整代码:
<?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 :(得分:0)
$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));
答案 1 :(得分:-1)
尝试使用&#34; ... {$ dozent-&gt; id};&#34;。用双引号和括号。