gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:22:55 24 4
gpt4 key购买 nike

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

目标是创建一个网络应用程序来记录我的患者咨询。因此,我的表结构是一个“主”表和两个与“主”表有关系的子表,名为“consults”和“procedures”。

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

以下是我能想到的最好的 MWE(如果有人有更简单的解决方案,我会很高兴)。第一个“患者日志”表运行良好,并且可以很好地显示患者行。这是问题所在的第二个表,特别是:

$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 变量,因此任何后续调用都会尝试针对 null 对象运行方法。

尝试删除对未设置的引用。

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

关于php - 使用 PHP、PDO 和 MySql 在一页上显示两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46425659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com