Banti Ram
Banti Ram

Reputation: 99

how to fetch an image from database in php

I am trying to show an image from the database to a new page using php. I am clicking on the image which needs to open in a new page. But everytime a same image is showing on the new page. I want an image in the new page which I clicked in last page. please help me.

<?php
    $con = mysql_connect("localhost", "root" , "");
    $sdb = mysql_select_db("display_images",$con);
    $sql = "SELECT image FROM `tab1` WHERE 1";
    $mq = mysql_query($sql) or die ("not working query");
    $row = mysql_fetch_array($mq) or die("line 44 not working");
    $s = $row['image'];
    echo $row['image'];

    echo '<img src="'.$s.'" alt="HTML5 Icon" style="width:800px;height:500px " >';
?>

Upvotes: 3

Views: 1375

Answers (1)

PHP dev
PHP dev

Reputation: 420

Kindly use the below code:

<?php

  $con=mysql_connect('localhost','root','');
  $coo=mysql_select_db('your_db',$con);
   if(!$coo)
    {
        echo 'error';
    }
   $get='select * from `your_table` where 1';

   $get1=mysql_query($get);

   $r=mysql_num_rows($get1);
   for($j=1;$j<=$r;$j++){
      $ans=mysql_fetch_array($get1);
?>
  <img src="<?php echo $ans['your_field_name']?>" width="200" height ="200" 
   attr="<? php echo $ans['your_field_name']?>" class="image"/>
 <?php } 
 ?>
 <script src="jquery.min.js" ></script>
 <script>   
     $( document ).ready(function(){

      $('.image').click(function(){

         var new_image= $(this).attr('attr');
         window.open( new_image );
    }); 
   });  
 </script>

The above code will do that just list out all the images from your table and then when you click a particular image that will displayed in the new window!!Is it your need?? mysql_query() is not preferable...Just for you!!

Upvotes: 1

Related Questions