PDO Connectivity Step 3

PDO Connectivity Step 3

3. Read Data – Index File

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

<!DOCTYPE html>
<html lang="en">
		<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">
<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>
	<div class="row">
		<div class="col-md-12">
			<div class="table-responsive">
			<table id="mytable" class="table table-bordred table-striped">

$sql = "SELECT * from pdo_practice";
//Prepare the query:
$query = $db->prepare($sql);
//Execute the query:
//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) {
			<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>

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

Post Your Comments & Reviews

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