Viewing Data from the Database in PHP
Let's create a page show.php,
where we can view the user's data
formatted with a specific layout.
Let's assume we are given the following layout for this:
<div>
<h1>user1</h1>
<p>
age: <span class="age">23</span>,
salary: <span class="salary">400</span>
</p>
</div>
Let the id of the user we want to
view be passed via the GET
parameter named id. Let's get it
into a variable:
<?php
$id = $_GET['id'];
?>
Let's form a query to get this user:
<?php
$query = "SELECT * FROM users WHERE id=$id";
?>
Let's execute the query:
<?php
$result = mysqli_query($link, $query) or die(mysqli_error($link));
?>
Let's write the user data into a variable:
<?php
$user = mysqli_fetch_assoc($result);
?>
Let's output this data in our layout:
<div>
<h1><?= $user['name'] ?></h1>
<p>
age: <span class="age"><?= $user['age'] ?></span>,
salary: <span class="salary"><?= $user['salary'] ?></span>
</p>
</div>
Implement the user view using the following layout:
<div>
<p>
name: <span class="name">user1</span>
</p>
<p>
age: <span class="age">23</span>,
salary: <span class="salary">400$</span>,
</p>
</div>
On the page index.php, implement the output
of links to view each of the users:
<a href="/en/show.php?id=1">user1</a>
<a href="/en/show.php?id=2">user2</a>
<a href="/en/show.php?id=3">user3</a>