如何以 Table 格式将 phpmyadmin 中的内容显示到我的 php 页面?
How do I display contents from phpmyadmin to my php page in a Table format?
这是我的代码,连接是在 dbConnect.php 中执行的(功能正常),你能给我指出正确的方向吗?显示我数据库信息的 php 代码在正文中,我看到其他人使用 sqli 但我似乎收到错误,因为我的数据库连接在 dbConnect.php 中。我对如何在不在此页面中插入数据库连接代码的情况下执行此操作感到有点困惑。提前致谢...
<?php
session_start();
require_once 'dbConnect.php';
$msgDia="";
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/newCss.css">
</head>
<body>
<!--Body Container-->
<div class="flex-container">
<!--Header Container-->
<header> <a href="tpaHomepage.php"><img src="images/logo.jpg" alt="logo"></a>
<div class="title">
<h1><b><i>Enabling cloud storage auditing with geolocation restrictions on an efficient key update</i></b></h1>
</div>
<ul><center>
<li><a href="tpaHomepage.php">Home</a></li>
<li><a href="activateusertpa.php">Activate Users</a></li>
<li><a href="auditfiles.php">Audit files</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="logout.php">Logout</a></li>
</center>
</ul></header>
<!--Login Container-->
<div class="loginForm">
<h2>Users Table</h2>
<p>The users who have recently registered to the system has been presented here for the TPA to enable the clients to access website.</p>
<?php
if ($_SESSION['tpaSession']!="") {
$query = $DBcon->query("SELECT * FROM usertbl");
$row=$query->fetch_array();
$count=$query->num_rows;
if ($count > 0) {
echo "<table><tr><th>ID</th><th>Firstname</th><th>Lastname</th></tr>";
while($row = $query) {
echo "<tr><td>" . $row["userid"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
} else {
header("Location: tpaLogin.php");
}
?>
<br>
</body>
</html>
如果您使用的是 mysqli,那么您的数据库连接应显示如下内容:
$DBcon = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
为了你的query/fetch:
$query = $DBcon->query("SELECT * FROM usertbl");
if($query->num_rows() > 0) {
echo"
<table><tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
</tr>
";
while($row = $query->fetch_assoc()) {
echo "<tr><td>$row[userid]</td><td>$row[firstname] $row[lastname]</td></tr>
";
}
echo "</table>";
}
else {
echo "0 results";
header("Location: tpaLogin.php");
}
我已将我的代码更改为以下内容并且运行良好,感谢指导 -NiallFH
<?php
if ($_SESSION['tpaSession']!="") {
$sql = "SELECT * FROM usertbl";
$result = mysqli_query($DBcon,$sql)or die(mysqli_error());
$count=$sql->num_rows;
if ($sql) {
echo "<table><tr><th>ID</th><th>Firstname</th><th>Lastname</th></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row["userid"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
}
else {
header("Location: tpaLogin.php");
}
?>
这是我的代码,连接是在 dbConnect.php 中执行的(功能正常),你能给我指出正确的方向吗?显示我数据库信息的 php 代码在正文中,我看到其他人使用 sqli 但我似乎收到错误,因为我的数据库连接在 dbConnect.php 中。我对如何在不在此页面中插入数据库连接代码的情况下执行此操作感到有点困惑。提前致谢...
<?php
session_start();
require_once 'dbConnect.php';
$msgDia="";
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/newCss.css">
</head>
<body>
<!--Body Container-->
<div class="flex-container">
<!--Header Container-->
<header> <a href="tpaHomepage.php"><img src="images/logo.jpg" alt="logo"></a>
<div class="title">
<h1><b><i>Enabling cloud storage auditing with geolocation restrictions on an efficient key update</i></b></h1>
</div>
<ul><center>
<li><a href="tpaHomepage.php">Home</a></li>
<li><a href="activateusertpa.php">Activate Users</a></li>
<li><a href="auditfiles.php">Audit files</a></li>
<li><a href="contact.php">Contact</a></li>
<li><a href="logout.php">Logout</a></li>
</center>
</ul></header>
<!--Login Container-->
<div class="loginForm">
<h2>Users Table</h2>
<p>The users who have recently registered to the system has been presented here for the TPA to enable the clients to access website.</p>
<?php
if ($_SESSION['tpaSession']!="") {
$query = $DBcon->query("SELECT * FROM usertbl");
$row=$query->fetch_array();
$count=$query->num_rows;
if ($count > 0) {
echo "<table><tr><th>ID</th><th>Firstname</th><th>Lastname</th></tr>";
while($row = $query) {
echo "<tr><td>" . $row["userid"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
} else {
header("Location: tpaLogin.php");
}
?>
<br>
</body>
</html>
如果您使用的是 mysqli,那么您的数据库连接应显示如下内容:
$DBcon = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
为了你的query/fetch:
$query = $DBcon->query("SELECT * FROM usertbl");
if($query->num_rows() > 0) {
echo"
<table><tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
</tr>
";
while($row = $query->fetch_assoc()) {
echo "<tr><td>$row[userid]</td><td>$row[firstname] $row[lastname]</td></tr>
";
}
echo "</table>";
}
else {
echo "0 results";
header("Location: tpaLogin.php");
}
我已将我的代码更改为以下内容并且运行良好,感谢指导 -NiallFH
<?php
if ($_SESSION['tpaSession']!="") {
$sql = "SELECT * FROM usertbl";
$result = mysqli_query($DBcon,$sql)or die(mysqli_error());
$count=$sql->num_rows;
if ($sql) {
echo "<table><tr><th>ID</th><th>Firstname</th><th>Lastname</th></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row["userid"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
}
else {
header("Location: tpaLogin.php");
}
?>