1

Hey I am trying to display an image whose local link is saved in MySQL database. I am writing a SQL query to select the required local link which is further stored in a variable to be passed to HTML image display function. The problem is that image is not being displayed rather short square containing cross symbol is being displayed.

// skipping the coding in which SQL connection is made and php script starting syntax

   <?php

   $sql = "SELECT locallink FROM timetable WHERE id=6";
   $result = $conn->query($sql);

   if ($result->num_rows > 0) {
   // output data of each row
    while($row = $result->fetch_assoc()) {
      $a = $row["locallink"];
    }
    }else {
     echo "0 results";
          }
   ?> 
    <!DOCTYPE html>
    <html>
    <body>

    <img src="<?php echo $a ?>" alt="Section A timetable">


    </body>
    </html>
Mafaz Ahsan
  • 105
  • 2
  • 11

2 Answers2

1

If u are running your server on local machine, then it won't be displayed as chrome not allow local resources to load

you may get helep from

Cannot open local file - Chrome: Not allowed to load local resource

SKYz
  • 67
  • 6
1

I found some issues with your code so I decided to make some modifications for you to review. If you are getting results back the below should work.

If you would be able to provide an example of locallink that would be helpful. You may also need to append "file:///" to the output of the $image variable.

<?php
$sql = "SELECT locallink FROM timetable WHERE id=6";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    /*
     * Output the data from each row into a array to form a collection. If 
     * we don't do this you will only end up with the last result in the while
     * loop.
     */
    $a = array();
    while ($row = $result->fetch_assoc()) {
        $a[] = $row["locallink"];
    }
}
else {
    echo "0 results";
}

/**
 * OutputImages
 * 
 * This function will take a collection of image locations and return a HTML 
 * markup string to the output buffer.
 * 
 * @param type array $images This should be an associative array.
 */
function OutputImages($images) {
    foreach ($images as $image) {
        echo '<img src="' . $image . '" alt="Section A timetable">';
        echo "<br>";
    }
}
?>
<!DOCTYPE html>
<html>
    <body>
        <?php
        /*
         * Now if we made it this far we can assume we have a collection of images
         * that we want to display. We can now call the OutputImages function.
         */
        OutputImages($a);
        ?>
    </body>
</html>