PDO Connectivity Step 3

PDO Connectivity Step 3

3. Read Data – Index File

<?php
include_once 'dbconfig.php';
try
{
    $database = new Connection();
    $db = $database->openConnection();
    //echo "Connection created successfully";
    $database->closeConnection();
} catch (PDOException $e) {
    echo "There is some problem in connection: " . $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>PHP CRUD Operations using PDO </title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body>
<div class="container p-5">
	<div class="row">
		<h3>PHP CRUD Operations using PDO Extension</h3>
		<div class="col-md-12 text-end">
		<a href="insert.php"><button class="btn btn-primary"> Insert Record</button></a>
		<hr>
	</div>
</div>
	<div class="row">
		<div class="col-md-12">
			<div class="table-responsive">
			<table id="mytable" class="table table-bordred table-striped">
				<thead>
					<th>ID</th>
					<th>Name</th>
					<th>City</th>
					<th>Edit</th>
					<th>Delete</th>
				</thead>
			<tbody>

<?php
$sql = "SELECT * from pdo_practice";
//Prepare the query:
$query = $db->prepare($sql);
//Execute the query:
$query->execute();
//Assign the data which you pulled from the database (in the preceding step) to a variable.
$results = $query->fetchAll(PDO::FETCH_OBJ);

if ($query->rowCount() > 0) {
//In case that the query returned at least one record, we can echo the records within a foreach loop:
    foreach ($results as $result) {
        ?>
		<tr>
			<td><?php echo htmlentities($result->id); ?></td>
			<td><?php echo htmlentities($result->name); ?></td>
			<td><?php echo htmlentities($result->city); ?></td>

			<td><a href="update.php?update=<?php echo htmlentities($result->id); ?>"><button class="btn btn-primary btn-xs">UPDATE</button></a></td>

			<td><a href="delete.php?del=<?php echo htmlentities($result->id); ?>"><button class="btn btn-danger btn-xs" onClick="return confirm('Do you really want to delete');">DELETE</button></a></td>
		</tr>
<?php
}}
?>
			</tbody>
		</table>
	</div>
</div>
		</div>
	</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*