3

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 " >';
?>
9
  • 2
    Please stop using mysql, use mysqli (PDO) is it far better and more secure. Commented May 26, 2015 at 10:00
  • Always use var_dump() to check what your variables store, if they store nothing insert your query directly into MySQL using phpMyAdmin and see what data is being selected, if none, fix your query. Commented May 26, 2015 at 10:02
  • When you want to print errors go about it like that: die('Error: ' . mysql_error()); Commented May 26, 2015 at 10:02
  • 1
    You need to send the id of the image as query string with link then use SELECT image FROM tab1 WHERE id='imageid'; Commented May 26, 2015 at 10:05
  • You need to get id for identifying which image is clicked and moreover you are not iterating images, it seems you are using very first row coming through query only, this way you will always be getting one image. Commented May 26, 2015 at 10:08

1 Answer 1

1

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!!

Sign up to request clarification or add additional context in comments.

5 Comments

I want to show a pic that was clicked. I dont want to show a same pic when i click any image. please help
you will be displayed more than one image?
i want the image to be showed which i clicked. but my code is showing the same image everytime whenever i click any image..i want u to please correct my code if there are any errors
yes I tried but it didnt work. Now whenever i am clicking an image, all the images from the database are showing on the new page. The pic that i clicked is not showing alone.
have you connect the jquery library file in the <script>? Post your tried code also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.