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();
}
}
http://php.net/manual/en/pdo.error-handling.php - http://php.net/manual/en/function.error-reporting.php –
ここで、 '$ dozent'と'$ dozent-> id'が定義されていますか? – VolkerK
1)一重引用符は*補間されません2)正しいコードを書いてください(プレースホルダ付き)(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq = 1)(この問題を完全に回避する) – user2864740