Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Noob question need help!!

    Hi guys. I'm new to PHP and MySQL and I am trying to put together a simple script that calls image urls from the database and places them as the img src in an html tag for viewing. So far, I have run into a few problems. First, when I run the query without a limitation of 1, the query sets ALL of my results for the entire table to the variable result_row['style]. Style is only one column in the table that contains the URL for one image, and there are multiple rows of 'style' that contain one url listing each, but the query returns everything into a single array element which is useless. Also, when I place the image into a html image tag, it won't display the image, even though the URL is perfectly valid and if checked in my browser will display the picture.

    Anyway, here is the code I hope you can tell me what is wrong

    Code:
    $result = mysql_query("SELECT style FROM GiftWrap WHERE GiftWrap = 1 ");
    if (!$result) 
    {
    die ("Impossible command". mysql_error());
    }
    
     while ($result_row = mysql_fetch_array(($result)))
    { 
    
    echo $result_row['style'];
    
    }
    
    
    
    ?>
    <html>
    <img src="<? echo $result_row['style'];?>" align = 'middle">
    
    
    </html>
    <?
    
    
    
    
    mysql_close($connection); 
    ?>

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,503
    Thanks
    72
    Thanked 105 Times in 104 Posts
    PHP Code:
    $result mysql_query("SELECT style FROM GiftWrap WHERE GiftWrap='1' LIMIT 1");

    $result2 mysql_fetch_array($result);

    echo 
    "<img src=\"".$result2['style']."\">"
    Assuming your images are stored inside an images folder.
    Last edited by myfayt; 09-12-2011 at 11:40 PM.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The style table returns the entire link i.e www.site.com/images/image.jpg
    Would I still need the extra images?

    And how would I fix the issue with all results being thrown into one element?
    The way it sits now, if I try to query more than one result, it looks like this

    echo $result['style'] << outputs: img1urlimg2urlimg3urlimg4url

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    Code:
    <html>
    <head>
        <title>pix</title>
    </head>
    <body>
    <?php
    
    // make your mysql connection here
    
    $result = mysql_query("SELECT style FROM GiftWrap WHERE GiftWrap = 1 ");
    if (!$result) 
    {
        die ("Impossible command". mysql_error());
    }
    while ($result_row = mysql_fetch_array(($result)))
    { 
        echo '<img src="' . $result_row['style'] . '" alt="picture" style="whatever" />';
    }
    // best to close result and connection here, but will work if you don't
    ?>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,503
    Thanks
    72
    Thanked 105 Times in 104 Posts
    Updated my post, try that. It will result full url, and only display 1.


  •  

    Posting Permissions

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