如何修复'Undefined variable:pdo;致命错误:未捕获错误:在null上调用成员函数query();'?

问题描述 投票:-1回答:1

我在下面的代码中被困了几个小时。我不知道如何解决这个错误。

注意:未定义的变量:第192行的C:\ xampp \ htdocs \ latihan2 \ update.php中的pdo致命错误:未捕获错误:在C:\ xampp \ htdocs \ latihan2 \ update中调用null上的成员函数query()。 php:192堆栈跟踪:在第192行的C:\ xampp \ htdocs \ latihan2 \ update.php中抛出#0 {main}

**//file config.php**


<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'demo2');

/* Attempt to connect to MySQL database */
try{
    $pdo = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME,     DB_USERNAME, DB_PASSWORD);
    // Set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    die("ERROR: Could not connect. " . $e->getMessage());
}
?>


**//file update.php line 186-236**


<?php  
    require_once "config.php";

    if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
        $id =  trim($_GET["id"]);
        $data_det = "SELECT * FROM employees_det WHERE id = :id";
        $result_det = $pdo->query($data_det);
        if($result_det->rowCount() > 0){

            echo "<div class='table-responsive'>";
            echo "<table class='table table-bordered table-striped'>";
                echo "<thead>";
                    echo "<tr>";
                        echo "<th>ID Parent</th>";
                        echo "<th>ID 2</th>";
                        echo "<th>Name</th>";
                        echo "<th>Job Level</th>";
                        echo "<th>Address</th>";
                        echo "<th>Salary</th>";
                        echo "<th>Action</th>";
                    echo "</tr>";
                echo "</thead>";
                echo "<tbody>";

            while($row = $result_det->fetch()){
                echo "<tr>";
                    echo "<td>" . $row['id'] . "</td>";
                    echo "<td>" . $row['id2'] . "</td>";
                    echo "<td>" . $row['name'] . "</td>";
                    echo "<td>" . $row['level'] . "</td>";
                    echo "<td>" . $row['address'] . "</td>";
                    echo "<td>" . $row['salary'] . "</td>";
                echo "</tr>";
            }
                echo "</tbody>";                            
            echo "</table>";
            echo "</div>";
            // Free result set
            unset($result_det);
        } else{
            echo "<p class='lead'><em>No records were found.</em></p>";
        }
    }
    // Close connection
    unset($pdo);
?>
php html
1个回答
0
投票

请检查此代码。您没有准备和执行您的代码。

<?php
error_reporting(E_ALL);
require_once "config.php";

if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
    $id =  trim($_GET["id"]);
    $data_det = "SELECT * FROM employees_det WHERE id = :id";
    $result_det = $pdo->prepare($data_det);
    $result_det->execute(['id'=>$id]);
    $result_det->setFetchMode(PDO::FETCH_ASSOC);
    if($result_det->rowCount()){

        echo "<div class='table-responsive'>";
        echo "<table class='table table-bordered table-striped'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>ID Parent</th>";
        echo "<th>ID 2</th>";
        echo "<th>Name</th>";
        echo "<th>Job Level</th>";
        echo "<th>Address</th>";
        echo "<th>Salary</th>";
        echo "<th>Action</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

        while($row = $result_det->fetch()) {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['id2'] . "</td>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['level'] . "</td>";
            echo "<td>" . $row['address'] . "</td>";
            echo "<td>" . $row['salary'] . "</td>";
            echo "</tr>";
        }
        echo "</tbody>";
        echo "</table>";
        echo "</div>";
        // Free result set
        unset($result_det);
    } else{
        echo "<p class='lead'><em>No records were found.</em></p>";
    }
}
// Close connection
unset($pdo);
?>
© www.soinside.com 2019 - 2024. All rights reserved.