include "config.php";
$id=8;
$student='Alex';
//$photo= fopen('photos/1.png', 'rb'); // reading binary data
$photo= file_get_contents('photos/1.png'); // reading binary data
$query="insert into student_profile(id,student,profile_photo)
values(:id,:student,:profile_photo)";
$step=$dbo->prepare($query);
$step->bindParam(':id',$id,PDO::PARAM_INT, 3);
$step->bindParam(':student',$student,PDO::PARAM_STR, 10);
$step->bindParam(':profile_photo',$photo,PDO::PARAM_LOB);
if($step->execute()){
echo " Data with Photo is added ";
}
else{
echo " Not able to add data please contact Admin ";
print_r($step->errorInfo());
}
include "config.php";
$query="SELECT * FROM student_profile";
echo "<table><tr><th>ID</th><th>Name</th>
<th>Profile</th></tr>";
foreach($dbo->query($query) as $row){
echo "<tr><td><a href=record-display.php?id=$row[id]>$row[id]</a></td>
<td>$row[student]</td><td>"
."<img src='data:image/jpeg;base64,".base64_encode($row[profile_photo]).
"'/></td>
</tr>";
}
echo "</table>";
Watch the line used to display the image using $row[profile_photo].
"<img src='data:image/jpeg;base64,".base64_encode($row[profile_photo])."'>"
$id=1;
$student='Alex';
$photo= fopen('photos/2.png', 'rb');
$query="UPDATE student_profile SET profile_photo=:profile_photo
WHERE id=:id";
$step=$dbo->prepare($query);
$step->bindParam(':id',$id,PDO::PARAM_INT, 3);
$step->bindParam(':profile_photo',$photo,PDO::PARAM_LOB);
if($step->execute()){
echo " Data with Photo is Updated";
}
else{
echo " Not able to add data please contact Admin ";
print_r($step->errorInfo());
}
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.