How to display data from database in web page

  viewdb.php

<!DOCTYPE html>

<html>

<head>

<title>Table with database</title>

<style>

table {

border-collapse: collapse;

width: 75%;

color: #588c7e;

font-family: monospace;

font-size: 25px;

text-align: left;

}

th {

background-color: #588c7e;

color: white;

}

tr:nth-child(even) {background-color: #f2f2f2}

</style>

</head>

<body>

<table>

<tr>

<th>Id</th>

<th>Firstname</th>

<th>Lastname</th>

<th>email</th>

</tr>

<?php

$conn = mysqli_connect("localhost", "root", "", "testdb");

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

$sql = "SELECT id, first_name, last_name,email FROM emp";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "<tr><td>" . $row["id"]. "</td><td>" . $row["first_name"] . "</td><td>"

. $row["last_name"] .  "</td><td>"

. $row["email"] .  "</td></tr>";

}

echo "</table>";

} else { echo "0 results"; }

$conn->close();

?>

</table>

</body>

</html>


viewdb1.php

<!DOCTYPE html>

<html>

<head>

<title>Table with database</title>

<style>

table {

border-collapse: collapse;

width: 75%;

color: #588c7e;

font-family: monospace;

font-size: 25px;

text-align: left;

}

th {

background-color: #588c7e;

color: white;

}

tr:nth-child(even) {background-color: #f2f2f2}

</style>

</head>

<body>

<table>

<tr>

<th>Id</th>

<th>Firstname</th>

<th>Lastname</th>

<th>email</th>

</tr>

<?php

$conn = mysqli_connect("localhost", "root", "", "testdb");

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}

$sql = "SELECT id, first_name, last_name,email FROM emp";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "<tr><td>" . $row["id"]. "</td><td>" . $row["first_name"] . "</td><td>"

. $row["last_name"] .  "</td><td>"

. $row["email"] .  "</td></tr>";

}

echo "</table>";

} else { echo "0 results"; }

$conn->close();

?>

</table>

</body>

</html>












Comments

Popular posts from this blog

How to create Animated 3d chart with R.

Linux/Unix Commands frequently used

R Programming Introduction