Trying to pull fields ‘info’ and ‘date’ from Comment table.
Two Tables:


Php
<?php
$pID2 = filter_input(INPUT_GET, 'pID', FILTER_SANITIZE_NUMBER_INT);
$username = "###";
$password = "####";
$pdo2 = new PDO('mysql:host=localhost;dbname=###', $username, $password);
$pdo2->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth2 = $pdo2->prepare('
SELECT info, date
FROM Professor, Comment
WHERE Professor.pID = ?');
$sth2->execute(array(
$pID2
));
?>
While Statement
while($row = $sth2->fetch(PDO::FETCH_ASSOC)) {
echo "<div class='comment'>by Anonymous on {$row['date']}: <br> {$row['info']} </div>";
}
The problem is, it pulls results, but it shows those two same comments for all the professors. Is there an error here with not calling a unique professor given the pID=[somenumber] in the url??
The actual SQL code should be:-
You need to put a
JOINstatement with theProfessortable.Hope it helps.
Update:-
Another checking should have been provided (as mentioned by the OP in the comment). If no rows are found, then the row count checking should be applied as:-
It’s always good to have this sort of a checking of row counts. Better checking than falling for coding errors. Best of Luck!