-1

I am creating a photo gallery using PHP and MYSQL. I want the mages and the captions under them to have a hyperlinks on them. The hyperlink for both the image and the caption under it will go to the same web page. How do I get images and their captions to display on a web page using php and mysql? It's important that the images and the captions have links on them. Image of the PhpMymin database and table This is the code:

<!DOCTYPE html>
<html>
<body>

<?php
mysql_connect ("localhost", "root", "");
mysql_select_db ("display_images");
$result = mysql_query("SELECT * FROM table1");


echo "<table>";

while ($row = mysql_fetch_array($result)) {

    echo "<tr>";
    echo "<td>"; ?> 
    echo '<a href="'$row ["imagelink"].'"><img src="img/'.echo $row ["images1"].'" width="150" height="150" alt=""> <br>'

echo $row ["caption"] </a>' </td>; echo "</td>";

    echo "</tr>";
  }

echo "</table>";



?>
</body>
</html>
Carl Max
  • 125
  • 1
  • 1
  • 10
  • pay close attention to your quoted and unquoted strings; check the difference between html to be echo'd and code to be executed. this is extremely important to learn immediately. – derelict Aug 27 '16 at 03:00
  • http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Tieson T. Aug 30 '16 at 03:42

1 Answers1

-1

Try below code for your need , In your code you are not using proper enquotes for html code and php variable ...

<!DOCTYPE html>
<html>
<body>

<?php
   mysql_connect("localhost", "root", "");
   mysql_select_db("display_images"); // Please check with your database name
   $result = mysql_query("SELECT * FROM table1");

   echo "<table>";

   while ($row = mysql_fetch_array($result)) 
   {
       $image_link = $row['imagelink'];
       $image_path = $row['image1'];
       echo "<tr>";
       echo "<td>";
       echo "<a href=".$image_link."><img src=".$image_path." width='150' height='150' alt='' /> <br>";
       echo $row['caption']."</a>"; 
       echo "</td>";
       echo "</tr>";
   }

   echo "</table>";

?>

I hope this will help you , Feel free to comment.

Mahamadali
  • 319
  • 3
  • 11