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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -