我正在尝试使用isset()可视化PHP中的变量,当我不使用它时,会出现一条通知,指出该索引未定义。
我正在尝试使用上述函数在表中输出变量,但是有了它,表将不输出任何值。
[
<?php
// Include config file
require_once "config.php";
// Attempt select query execution
$sql = "SELECT * FROM courses";
if($result = $pdo->query($sql)){
if($result->rowCount() > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Enquiry ID</th>";
echo "<th>Course Name</th>";
echo "<th>Course Level</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = $result->fetch()){
echo "<tr>";
echo "<td>" . isset($row\['courseid'\]) . "</td>";
echo "<td>" . isset($row\['enquiryid'\]) . "</td>";
echo "<td>" . isset($row\['coursename'\]) . "</td>";
echo "<td>" . isset($row\['courselevel'\]) . "</td>";
echo "<td>";
echo "<a href='read.php?id=". isset($row\['courseid'\]) ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". isset($row\['courseid'\]) ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". isset($row\['courseid'\]) ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// 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);
?>
] 1
我希望输出数据库表中包含的值,但是这些字段仍为空白。
答案 0 :(得分:3)
isset()
返回true或false。您无法直接将其回显。您需要一个条件条件,该条件条件使用isset()
来检查变量并将其显示为true。
假设您正在运行PHP 7,则可以使用null合并运算符。这是三元数的简写形式,根据是否设置了变量,该三元数会导致变量或空白字符串的出现:
echo "<td>" . $row['courseid'] ?? '' . "</td>";
文档:https://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
这等效于:
echo "<td>" . isset($row['courseid']) ? $row['courseid'] : '' . "</td>";
但是,如果您被迫使用该三元方法,为了方便起见,我可能会将其包装在一个函数中。
答案 1 :(得分:0)
此isset()
函数返回True / False,因此您不能直接echo
将该值删除。
因此最好在isset()
就像我在下面写的
echo "<td>" . isset($row['courseid']) ? $row['courseid'] : 'Not Set' . "</td>";
答案 2 :(得分:-1)
我以您的while循环为例:
while($row = $result->fetch()){
echo "<tr>";
if (isset($row['courseid'])){
echo "<td>" . $row['courseid']) . "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
if (isset($row['enquiryid'])){
echo "<td>" . $row['enquiryid'] . "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
if (isset($row['coursename'])){
echo "<td>" . $row['coursename']. "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
if (isset($row['courselevel'])){
echo "<td>" . $row['courselevel']) . "</td>";
} else {
echo "<td>" . 'NOT SET' . "</td>";
}
echo "<td>";
echo "<a href='read.php?id=". $row['courseid'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". $row['courseid'] ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row\'courseid'] ."' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
希望有帮助