使用 PHP、PDO 和 MySql 在一页上显示两个表格

Two tables displayed on one page using PHP, PDO and MySql

我正在尝试创建一个 CRUD 仪表板,使用 MySql 后端和带有 PHP 的 bootstrap front-end 和 PDO 与数据库通信。我是网络开发的菜鸟,但不是编码的菜鸟。

目标是创建一个网络应用程序来记录我的患者咨询。因此,我的 table 结构是一个 "main" table 和两个 children table 与 "main" table 的关系,命名为 "consults" 和 "procedures".

我正在尝试制作一个仪表板,在其中显示我的 "main" table,然后在其下方添加两个 children table。 (稍后我会更好地设计它,但我正在努力让它发挥作用)。

以下是我能想到的最好的 MWE(如果有人有更简单的解决方案,我会很高兴)。第一个 "logbook patients" table 效果很好,并且很好地显示了患者的行。问题是第二个 table,特别是:

$sql = "SELECT * FROM proc";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){

这是我一直出错的地方。错误是:

Fatal error: Uncaught Error: Call to a member function query() on null in /home/paincl5/public_html/logbook/logbook.php:110 Stack trace: #0 {main} thrown in /home/paincl5/public_html/logbook/logbook.php on line 110

第110行的代码是

unset($pdo);

我的完整代码是:

<div class="wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="page-header clearfix">
                    <h2 class="pull-left">Logbook Patients</h2>
                    <a href="create.php" class="btn btn-success pull-right" >Add New Patient</a>


                </div>
                <?php
                // Include config file
                require_once 'config.php';

                // Attempt select query execution
                $sql = "SELECT * FROM main";
                if($result = $pdo->query($sql)){
                    if($result->rowCount() > 0){
                        echo "<div style='height:300px;overflow-y:scroll;;'>";
                        echo "<table class='table table-bordered table-striped'>";
                            echo "<thead>";
                                echo "<tr>";
                                    echo "<th>Surname</th>";
                                    echo "<th>first_name</th>";
                                    echo "<th>DOB</th>";
                                    echo "<th>Hospital</th>";
                                    echo "<th>MRN</th>";
                                    echo "<th>Action</th>";
                                echo "</tr>";
                            echo "</thead>";
                            echo "<tbody>";
                            while($row = $result->fetch()){
                                echo "<tr>";
                                    echo "<td>" . $row['Surname'] . "</td>";
                                    echo "<td>" . $row['first_name'] . "</td>";
                                    echo "<td>" . $row['DOB'] . "</td>";
                                    echo "<td>" . $row['Hospital'] . "</td>";
                                    echo "<td>" . $row['MRN'] . "</td>";
                                    echo "<td>";
                                        echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                                        echo "<a href='update.php?id=". $row['id'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                                        echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                    echo "</td>";
                                echo "</tr>";
                            }
                            echo "</tbody>";                            
                        echo "</table>";
                        echo "</div>";
                        // Free result set
                        unset($result);
                    } else{
                        echo "<p class='lead'><em>No records were found.</em></p>";
                    }
                } else{
                    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
                }

                // Close connection
                unset($pdo);
                ?>
            </div>
        </div>        
    </div>
</div>



// Procedure Table
 <div class="wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="page-header clearfix">
                    <h2 class="pull-left">Procedures</h2>
                    <a href="create_proc.php" class="btn btn-success pull-right" >Add New Procedure</a>
                </div>
                <?php
                // Include config file
                require_once 'config.php';
                // Attempt select query execution
                $sql = "SELECT * FROM proc";
                if($result = $pdo->query($sql)){
                    if($result->rowCount() > 0){
                        echo "<div style='height:300px;overflow-y:scroll;;'>";
                        echo "<table class='table table-bordered table-striped'>";
                            echo "<thead>";
                                echo "<tr>";
                                    echo "<th>Procedure Type</th>";
                                    echo "<th>Procedure Name</th>";
                                    echo "<th>Notes</th>";
                                    echo "<th>Action</th>";
                                echo "</tr>";
                            echo "</thead>";
                            echo "<tbody>";
                            while($row = $result->fetch()){
                                echo "<tr>";
                                    echo "<td>" . $row['procedure_type'] . "</td>";
                                    echo "<td>" . $row['procedure_name'] . "</td>";
                                    echo "<td>" . $row['notes'] . "</td>";
                                    echo "<td>";
                                        echo "<a href='update.php?id=". $row['id1'] ."' title='Update Record' data-toggle='modal' data-target='#myModal' ><span class='glyphicon glyphicon-pencil'></span></a>";
                                        echo "<a href='delete.php?id=". $row['id1'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                    echo "</td>";
                                echo "</tr>";
                            }
                            echo "</tbody>";                            
                        echo "</table>";
                        echo "</div>";
                        // Free result set
                        unset($result);
                    } else{
                        echo "<p class='lead'><em>No records were found.</em></p>";
                    }
                } else{
                    echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
                }

                // Close connection
                unset($pdo); //Here occurs the error (line 110)
                ?>
            </div>
        </div>        
    </div>
</div>

您正在使用 unset($pdo) 破坏您的 $pdo 变量,因此任何后续调用都试图对空对象使用 运行 方法。

尝试删除对 unset 的引用。

我假设 $pdo 对象来自 config.php。由于您使用的是 require_once,它只会在第一次调用 require_once 时包含配置文件。这就是 $pdo 被销毁而不是重新创建的原因。