Skip to content

Instantly share code, notes, and snippets.

@macloo
Last active July 12, 2017 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macloo/fcd562cd42075e8f8a0a to your computer and use it in GitHub Desktop.
Save macloo/fcd562cd42075e8f8a0a to your computer and use it in GitHub Desktop.
Example of PHP used (with no JavaScript) to query (read) a database and then write results within HTML
<?php include 'database.php'; ?>
<?php
$query = "SELECT * FROM shouts ORDER BY id DESC LIMIT 20";
$shouts = mysqli_query($conn, $query);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name=viewport content="width=device-width, initial-scale=1">
<title> Read the Shoutbox DB </title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div id="container">
<header>
<h1>My Shoutbox</h1>
</header>
<div id="shouts">
<ul>
<!-- begin PHP while-loop to display database query results
with each row enclosed in LI tags -->
<?php while($row = mysqli_fetch_assoc($shouts)) : ?>
<li>
<?php echo $row['name']; ?>:
<?php echo $row['shout']; ?>
[<?php echo $row['date']; ?>]
</li>
<?php endwhile; ?>
<!-- end the PHP while-loop
everything else on this page is normal HTML -->
</ul>
</div>
<footer>
<p style="text-align: center;">Read-only example from a MySQL database</p>
</footer>
</div> <!-- close container -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment