postheadericon Part-4: Display Records using prepared statement



Part-4: Display Records using prepared statement.



In part four, we concentrate on fetching record from database using prepared statement. We use select SQL statement to retrieve all record from table. After executing our query using PDO object we check whether it return the rows or not. If it does not return records then “No record found.” Message will be shown otherwise record will be display in HTML table. Last two column have link for Edit and Update record. When user click on it, it pass id to edit or delete page using default GET method. 



display.php


<?php
require "conn.php";

try 
{
    
$stmt = $conn->prepare("SELECT id, name, email, phone FROM tbl"); 
$stmt->execute();

# setting the fetch mode  
$result = $stmt->fetchAll();
if( ! $result)
{
print('No Records Found');
else
{
echo "<table border='1' align='center' cellpadding='5px' cellspacing='2px'>";
//while($row = $stmt->fetch()) 
echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th colspan='2'></th></tr>";
foreach($result as $row)
{
echo "<tr>";
echo "<td>";
echo $row['id'];
echo "</td>";
echo "<td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo $row['email'];
echo "</td>";
echo "<td>";
echo $row['phone'];
echo "</td>";
echo "<td>";
$search_str=$row['id'];
echo "<a href='edit.php?id=$search_str'>Edit</a>";
echo "</td>";
echo "<td>";
$search_str=$row['id'];
echo "<a href='delete.php?id=$search_str'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
}
catch(PDOException $e) 
{
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>


1 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.