Php Error on Query
Block1
<?php
// Filter our input.
$dID = filter_input(INPUT_GET, 'dID', FILTER_SANITIZE_NUMBER_INT);
if(!$dID) {
echo "<h2 style='color:red;'>Invalid Department</h2>";
exit;
}
$username = "###";
$password = "####";
$pdo = new PDO('mysql:host=localhost;dbname=####', $username, $password);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $pdo->prepare('
$sth = $pdo->prepare(' SELECT name, fname, lname, picpath, email FROM Department, Professor WHERE Department.dID = ? '); '); $sth->execute(array( $dID ));
?>
Block2
<?php
echo "<span>{$row['name']}";
// Did we get any professors in this dept?
while($row2 = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<span>{$row['fname']} | {$row['lname']} | </span>"
; // echo
}
unset($sth);
?>
Output Given:
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |开发者_如何学Python | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Dept Table:
Prof Table:
It doesnt seem to like my query, not sure why though, its not pulling anything from the database. Could it be an sql error? I'm stumped
When I test in PhpmyAdmin it gives me:
UPDATED QUERY: SELECT name, fname, lname, picpath, email FROM Department, Professor WHERE Department.dID = Professor.dID AND Department.dID = ?
It still doesnt work though. Basically the page has for example in the url: '/dept.php?dID=30' it grabs the dID and is supposed to display all professors in a department. Anyone???
In your while
loop
while($row2 = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<span>{$row['fname']} | {$row['lname']} | </span>"
; // echo
}
you are using $row2 but in the echo
you are using $row['fname']
Have you tried directly executing the query against the database? I'm no PHP coder, and most of my SQL experience is T-SQL, but your where clause says WHERE WHERE dID = ?
- I'm guessing it could be a couple things. First off, unless it's a special MySql thing, the question mark prolly won't get you what it is you're after. Also, dID in this case would be an ambiguous reference, since both tables appear to have dID columns. Also, I'm thinking (once again, unless MySql works different from T-SQL) that you're after a join rather than putting Department.dID = Professor.dID
in your where clause. Also, to reiterate, executing this directly before putting it into your PHP would probably get you some good errors to go from in figuring out why your query isn't working.
精彩评论