开发者

Php Error on Query

开发者 https://www.devze.com 2023-02-21 06:58 出处:网络
Block1 <?php // Filter our input. $dID = filter_input(INPUT_GET, \'dID\', FILTER_SANITIZE_NUMBER_INT);

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:

Php Error on Query

Prof Table:

Php Error on Query

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:

Php Error on Query

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消