【发布时间】:2021-01-03 15:09:34
【问题描述】:
我有一个在 phpmyAdmin 上创建的数据库。 我有页面正在显示的问题
错误:无法执行 SELECT * FROM employees。
但我似乎把所有事情都做得很完美,不是吗?
所以,请看一下,这将是一个很大的帮助。
无法执行 SELECT * FROM employees。
这是我用来添加数据并在浏览器上显示数据的代码:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "HD";
$pdo = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<style type="text/css">
.wrapper{
width: 650px;
margin: 0 auto;
}
.page-header h2{
margin-top: 0;
}
table tr td:last-child a{
margin-right: 15px;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<h2 class="pull-left">Employees Details</h2>
<a href="create.php" class="btn btn-success pull-right">Add New Employee</a>
</div>
<?php
// Include config file
require_once "config.php";
// Attempt select query execution
$sql = "SELECT * FROM employees";
?>
<?php if($result = $pdo->query($sql)) {
if($result->rowCount() > 0) :?>
<table class='table table-bordered table-striped'>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Address</th>
<th>Salary</th>
<th>Designation</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch()): ?>
<tr>
<td> <?php echo $row['id'] ?> </td>
<td> <?php echo $row['name'] ?> </td>
<td> <?php echo $row['address'] ?> </td>
<td> <?php echo $row['salary'] ?> </td>
<td> <?php echo $row['designation'] ?> </td>
<td>
<a href='read.php?id= <?php echo $row['id'] ?>' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>
<a href='update.php?id=<?php echo $row['id']?>' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>
<a href='delete.php?id=<?php echo $row['id'] ?>' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>
</td>
</tr>
<?php endwhile ?>
</tbody>
</table>
<?php
// Free result set
unset($result);
?>
<?php else: ?>
<p class='lead'><em>No records were found.</em></p>
<?php endif ?>
<?php
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
unset($pdo);
?>
</div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
从 phpmyadmin 查询是否有效?
-
另外,为什么要使用两种不同类型的 if() 子句——一种使用
{和},另一种使用:和endif?这似乎让我感到不必要的困惑。 -
@droopsnoot ,是的,它正在为它工作
-
尝试删除尽可能多的代码,同时仍然查询数据库。看看stackoverflow.com/help/minimal-reproducible-example
-
如果适合您,请验证答案..
标签: php phpmyadmin