如何修复"未定义的变量:pdo;致命错误:未捕获错误:在 null;'?



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

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

**//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
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);
?>

最新更新