I created a MySQL database with a table using phpmyadmin. I created this table with a BLOB column to hold a jpeg file.
I have issues with regards to the php variable $result
here.
My code so far: (catalog.php):
<body> <?php $link = mysql_connect("localhost", "root", ""); mysql_select_db("dvddb"); $sql = "SELECT dvdimage FROM dvd WHERE id=1"; $result = mysql_query("$sql"); mysql_close($link); ?> <img src="" width="175" height="200" /> </body>
How can I get the variable $result from PHP into the HTML so I can display it in the <img>
tag?
php $id = $_GET['id']; // do some validation here to ensure id is safe $link = mysql_connect("localhost", "root", ""); mysql_select_db("dvddb"); $sql = "SELECT dvdimage FROM dvd WHERE id=$id"; $result = mysql_query("$sql"); $row = mysql_fetch_assoc($result); mysql_close($link); header("Content-type: image/jpeg"); echo ...
First, connect to the database: $conn=mysql_connect("hostname","username","password"); mysql_select_db("databasename",$conn); You can use this to display a single record: For example, if the URL was /index.
php $connection =mysql_connect("localhost", "root" , ""); $sqlimage = "SELECT * FROM userdetail where `id` = '". $id1. "'"; $imageresult1 = mysql_query($sqlimage,$connection); while($rows = mysql_fetch_assoc($imageresult1)) { echo'<img height="300" width="300" src="data:image;base64,'.
You can't. You need to create another php script to return the image data, e.g. getImage.php. Change catalog.php to:
<body> <img src="getImage.php?id=1" width="175" height="200" /> </body>
Then getImage.php is
<?php $id = $_GET['id']; // do some validation here to ensure id is safe $link = mysql_connect("localhost", "root", ""); mysql_select_db("dvddb"); $sql = "SELECT dvdimage FROM dvd WHERE id=$id"; $result = mysql_query("$sql"); $row = mysql_fetch_assoc($result); mysql_close($link); header("Content-type: image/jpeg"); echo $row['dvdimage']; ?>
Technically, you can too put image data in an img tag, using data URIs.
<img src="data:image/jpeg;base64,<?php echo base64_encode( $image_data ); ?>" />
There are some special circumstances where this could even be useful, although in most cases you're better off serving the image through a separate script like daiscog suggests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With