How To Do Reading/Listing Operations In PHP ?

By admin
In PHP
Nisan 28, 2022
4 min read

Hello everyone,

Today we will teach how to do crud operations in php. Above all, i would like to explain all our processing step by step.

HOW TO DO READING OPERATIONS IN PHP ?

  • Firstly, we have need to data. So, we will create sample data on mockaroo.
  • Then, we will create a new database on the Mysql.
  • Then, we will connect the database we have created with our PHP file.
  • Then, with the foreach loop, we will print the data from the database into the table we created.

READ

FIRST STEP

I created a table as personal data. This table have 4 columns. This columns ; id, first_name, last_name and email.

SECOND STEP

I will create a connect.php file for the connection to mysql database.

<?php
try {
$db = new PDO("mysql:host=localhost;dbname=personal", "root", "");
} catch ( PDOException $e ){
print $e->getMessage();
}
?>

THIRD STEP

I will create a index.php file for the list our mysql data. And i will include the con.php file I created insert this page. After created index.php before we will include con.php this page.

<?php

//We include our database file
include 'connect.php';

//We connect to the database and write our query
$query = $db->prepare("select * from personal");

$query->execute();
//We pass our data to array

$takequery = $query->fetchAll(PDO::FETCH_OBJ);

?>

FOURTH STEP

Then we will create a new bootstrap page and we will add bootstrap table. Shortly after We will list the data in the database in this table

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<title>LIST SCREEN</title>
</head>
<body>
<h1 class="display-1 text-center">USER LIST</h1>
<div class="container">
<div class="row">
<table class="table table-striped table-hover mt-3">

<tr>
<td>ID</td>
<td>NAME</td>
<td>CITY</td>
<td>E-MAIL</td>
</tr>

<?php

//We loop the data we transferred to the array in the table

foreach ($takequery as $queries) {?>

<tr>
<td><?= $queries->id ?></td>
<td><?= $queries->name?></td>
<td><?= $queries->city?></td>
<td><?= $queries->email?></td>

</tr>

<?php } ?>

</table>
</div>
</div>

 

 

<!-- Optional JavaScript; choose one of the two! -->

<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
-->
</body>
</html>

That’s all! 😀

You did it!

This is screen!

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir