mysql - Php - Display image from Database using path (PDO) -
i tried display image database,
- image in directory
path in database
require_once "connection.php"; class displaydataimageprofile { function showimageprofile(){ $connection = new connection(); $conn = $connection->getconnection(); $id = $_get['id']; try{ $sqldisplay = "select photo frofile id =$id"; $getimage = $conn->prepare($sqldisplay); $getimage->execute(); $getimage->fetchall(pdo::fetch_assoc); foreach($getimage $data){ header('content-type: image/jpg'); // echo "<img src='$data'>"; echo $data; } }catch (pdoexception $e){ echo "error : " + $e->getmessage(); } }}
after call in html page :
<img src="displaydataimageprofile .php?id=3" align="center" />
i got problem image cannot retrieve database using path. other case on webpage image broken image displayed.
for display 1 image.
you should use proper method.
instead of fetchall() returns nested array, have use fetchcolumn() returns single value. , should using prepared statements properly:
$sql = "select photo frofile id = ?"; $getimage = $conn->prepare($sqldisplay); $getimage->execute([$_get['id']]); header('content-type: image/jpg'); echo $getimage->fetchcolumn();
edit: if don't have image in database path image, don't need code @ all. rid of display.php , select path in script echoing
<img src="display.php?id=3" align="center" />
and echo selected path instead of display.php?id=3
.
Comments
Post a Comment