How do I display contents from phpmyadmin to my php page in a Table format?
Get the solution ↓↓↓This is my code, the connection is performed in dbConnect.php (which functions correctly), could you guy point me in the right direction. The php code to display the information from my database is in the body, I've seen others use sqli but I seem to get an error because my db connection is in the dbConnect.php. I'm just a little confused on how I could do this without inserting the database connection code in this page. Thanks in advance...
<?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>
Answer
Solution:
If you're using mysqli, then your DB connect should show something like:
$DBcon = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
For your 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");
}
Answer
Solution:
I've changed my code to the following and it worked perfectly well, thanks for the guide -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");
}
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.