Hi i am quite new to php, and am in the process of building an ecommerce photography website using php and mysql. I want to display 10 of the latest uploaded images on the "Photos" page. Below is the code, but for some reason only the links are displaying but not the photos.
<?php
require_once ('dbconnection.php');

$query = mysql_query ("SELECT * FROM image ORDER BY dateadded LIMIT 10");

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

echo "<a href=\"" . $row["imagepath"] . "\"><img src=\"" . $row["imagepath"] . "\" border=0 alt=\"" . $row["imagename"] . "\"></a>";
}
mysql_close();
?>

Any ideas about whats wrong?

Thanks in advance

    is the image path correct? check what is actually being outputted

      dagon wrote:

      is the image path correct? check what is actually being outputted

      Yes the image paths are correct since they are added into the db automatically when the image is uploaded onto the server directory.
      The output on the webpage is 10 links(names of the images). when clicked on they go to the path of the image itself but images is not displayed still.

        dagon wrote:

        is the image path correct?

        check what is actually being outputted

        He means you should look at the HTML: View Source

        There is nothing wrong with your code, by my testing.
        I get this output, if using path and name as variable values

        <a href="path"><img src="path" border=0 alt="name"></a>

        This should work if image exists.

        So, would say your query does not return correct IMAGE PATH.
        Here is the code I used for my test:

        <?php
        
        $row["imagepath"] = 'path';
        $row["imagename"] = 'name';
        
        echo "<a href=\"" . $row["imagepath"] . 
        "\"><img src=\"" . $row["imagepath"] . 
        "\" border=0 alt=\"" . $row["imagename"] . "\"></a>";

        regars 🙂
        halojoy

          halojoy wrote:

          He means you should look at the HTML: View Source

          There is nothing wrong with your code, by my testing.
          I get this output, if using path and name as variable values

          <a href="path"><img src="path" border=0 alt="name"></a>

          This should work if image exists.

          So, would say your query does not return correct IMAGE PATH.
          Here is the code I used for my test:

          <?php
          
          $row["imagepath"] = 'path';
          $row["imagename"] = 'name';
          
          echo "<a href=\"" . $row["imagepath"] . 
          "\"><img src=\"" . $row["imagepath"] . 
          "\" border=0 alt=\"" . $row["imagename"] . "\"></a>";

          regars 🙂
          halojoy

          its not working at all. I have tried using your testing code above. This is my new code now...

          <?php
          require_once ('dbconnection.php');

          $query = mysql_query ("SELECT * FROM image ORDER BY dateadded");

          $row["imagepath"] = 'path';
          $row["imagename"] = 'name';

          echo "<a href=\"" . $row["imagepath"] .
          "\"><img src=\"" . $row["imagepath"] .
          "\" border=0 alt=\"" . $row["imagename"] . "\"></a>";

          mysql_close();
          ?>

          now all it displays on the page is "name" link and nothing else.
          Please can you help. I have checked the image paths in the page source, and they match the ones in the directory, so that is not the problem. I want to display thumbnails of the images on this page, and when user clicks on the thumbnail a new page with the big image appears.

          Thank you, i really appreciate your help.

            ga86 wrote:

            iI have checked the image paths in the page source, and they match the ones in the directory,

            therefore its not a php issue more likley a html issue, how about telling us the actual page url.

              href requires a URL not a path so check that..

              also echo the imagepath variable by itself, copy it into your browser. if the image does not show up then it's wrong in the database.

                foyer wrote:

                href requires a URL not a path so check that..

                also echo the imagepath variable by itself, copy it into your browser. if the image does not show up then it's wrong in the database.

                i tried again but now it is not showing anything.

                $path = "http://www.doc.gold.ac.uk/~999000/uploads/";

                echo "<img src=\"{$path}{$img}\">";

                $query = mysql_query ("SELECT * FROM image ORDER BY dateadded");

                while ($row = mysql_fetch_array($query))
                {
                $img = $row['image'];

                echo "<img src=\"{$path}{$img}\">";

                }

                im the image table, there is a field called imagepath which stores the name of the directory where the images are stored. if i change this to a url, then i cannot upload images, so i didnt change that. Note that i have modified the url above from my student id to 999000, since i do not want to display my student number on a public forum.

                Please can you help me.

                  can you please show some actual output

                    dagon wrote:

                    can you please show some actual output

                    It is now showing a blank page, theres no output on it right now.

                      it should at least be showing "<img src .." etc, view source

                        It doesnt show a single thing on the page. I do not understand this.

                        Do you know any good tutorials i can use to retreive and display the images from the paths saved in the databse onto the webpage? I do not have alot of time left, my assignment is due soon. I think i am completely lost now, and maybe it will be better to start again from scratch.

                        I appreciate your time and help, Thank you.

                          before you even try to display the image you should check to see if it is there.

                          if (!file_exists($imageurl)) {
                          die('Image does not exist');
                          }

                            page source not, output to browser, for the third time

                              foyer wrote:

                              what..

                              lets say this is his php script

                              <?php
                              echo "<img src=\"{$path}{$img}\">";
                              ?>
                              

                              call that script.php

                              www.whatever.com/script.php

                              output to screen will be blank (ok you may be luckey as its an image tag and get a red X)

                              view source

                              you will get:

                              <img src="">

                              --- that tells you something, thats infinitely more informative, looking at the html after its interpreted by the browser is not very helpful at all.

                              you could get pages of output generated by a php script but, not actually shown by the browser

                                yeah so that's exactly why I would print the variable alone not inside any HTML tags.

                                It's called troubleshooting and a lot of people on this website do not know how to do it.

                                  even if you go print $var, it may not show depending on what other code\ html may be on the page -so always look at the source!

                                    well yeah there is a million ways to do it but the point is people don't do it enough

                                      people are idiots, they should all be taken out back and shot