SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    there is no box baztorres's Avatar
    Join Date
    May 2004
    Location
    UK - London \ Surrey
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    image file not being displayed when using my_sql_fetch_array

    Hi,
    I'm just getting into PHP\MySQL and I have hit an issue with displaying a sql query.
    What I'm trying to do is create a gallery, so there is a table that has the following in it:
    | photo_id | photo_filename | photo_caption | photo_category |

    The code in use to display the query is:

    PHP Code:
       while($row mysql_fetch_array($result)) {
         echo 
    "imageID" " " $row['photo_id'] . "<br />";
         echo 
    "image" $row['<img src='."photo_filename".'>'];
         echo 
    "<br />";
         echo 
    "image_caption" " " $row['photo_caption'];
       } 
    When running \ executing the page, only the image file is not displayed.

    How do I get php to get the image name inserted into the <img> tag in the echo?

    Ta,
    Baz
    Baz
    ---

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $rResult 
    mysql_query("SELECT photo_id, photo_filename, photo_caption FROM gallery");
    while(
    $aRecord mysql_fetch_assoc($rResult))
    {
        
    printf(
            
    '<div>
                <h4>ID: %s</h4>
                <img src="%s" />
                <p>%s</p>
            </div>'
    ,
            
    $aRecord['photo_id'],
            
    $aRecord['photo_filename'],
            
    $aRecord['photo_caption']
        );
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    there is no box baztorres's Avatar
    Join Date
    May 2004
    Location
    UK - London \ Surrey
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Thanks for the really quick reply, but it didn't return \ display anything...
    What is the printf() doing?

    Ta
    Baz
    ---

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you post more of the code you are using?

    See the manual for printf().
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    there is no box baztorres's Avatar
    Join Date
    May 2004
    Location
    UK - London \ Surrey
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, it is:
    Code:
    <body>
    
    
    
    <div id="container">
    
    		
    
    		<!-- Preserves space for the Header -->
    
    		<div id="pageHeader">
    
    		<span></span>
    
    		</div>
    
    		
    
    		<div id="content">
    
    		  <h3>Gallery:- Cateogry: Engine</h3>
    
    
    
    <?php
    
        $con = mysql_connect('localhost','root','');
        if (!$con) {
          die("Not happening, because: " . mysql_error());
        }
        echo "You're in <br />";
    
        mysql_select_db("george", $con);
    
    
        //MySQL query
        $result = mysql_query("SELECT * FROM gallery_photos");
    
       while($row = mysql_fetch_array($result)) {
         echo "imageID" . " " . $row['photo_id'] . "<br />";
         echo "image" . $row['<img src='."photo_filename".'>'];
         echo "<br />";
         echo "image_caption" . " " . $row['photo_caption'];
         echo "<hr /> <br />";
       }
    
       mysql_close($con);
    
       mysql_close($con);
     
    
    ?>
    
    
    
        </div>
    
    </body>
    Thanks, reading now....
    Baz
    ---

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What do you get with the following?
    PHP Code:
    <?php
    $rConn 
    mysql_connect('localhost''root');
    if(
    is_resource($rConn))
    {
        
    mysql_select_db('george');
        if(
    mysql_errno() === 0)
        {
            
    $rResult mysql_query("SELECT photo_id, photo_filename, photo_caption FROM gallery_photos");
            if(
    is_resource($rResult))
            {
                if(
    mysql_num_rows($rResult) > 0)
                {
                    while(
    $aRecord mysql_fetch_assoc($rResult))
                    {
                        
    printf(
                            
    '<div>
                                <h4>ID: %s</h4>
                                <img src="%s" />
                                <p>%s</p>
                            </div>'
    ,
                            
    $aRecord['photo_id'],
                            
    $aRecord['photo_filename'],
                            
    $aRecord['photo_caption']
                        );
                    }
                }
                else
                {
                    echo 
    'No records found.';
                }
            }
            else
            {
                die(
    mysql_error());
            }
        }
        else
        {
            die(
    mysql_error());
        }
    }
    else
    {
        die(
    mysql_error());
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    there is no box baztorres's Avatar
    Join Date
    May 2004
    Location
    UK - London \ Surrey
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, just noticed in the snippet that you did for me, the table wasn't correct. Corrected this and it displays the images:-D
    Thanks for your help.
    I think I understand the usage behind printf().

    Thanks again.
    Baz
    ---

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Bah! Great news though.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •