This query is only returning the first row twice, and not the whole array.

Discussion in 'PHP' started by dmanto2two, Jan 23, 2010.

  1. #1
    So this code should call a the name of a photo out of a database. then put it into an array then open the photos with the names in the array. the problem is that it only calls the first row twice. any ideas on how to fix this cause i'm stumped.
    the code is
    function display_photos ()
    {
    $valid_user = $_SESSION['valid_user'];
    mysql_connect("-------","-------","----");
    mysql_select_db("-----") or die("Unable to select database");
    $conn = db_connect();
    $result = mysql_query("select photo from photo where email='".$valid_user."' ");

    $a = mysql_fetch_array($result) or die("No photos holmes");

    ?>
    <table>
    <?php
    foreach($a as $b) {
    echo "$b";

    ?>
    <tr><td> <img src="images/<?php echo $b; ?>" alt="<?php echo $b; ?>" /> </td></tr>

    <?php
    }
    ?>
    </table>
    <?php
    }
    ?>
    anyway, any help would well... help. tanks
     
    dmanto2two, Jan 23, 2010 IP
  2. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    come to think of it i have a similar problem with a search function also.
     
    dmanto2two, Jan 23, 2010 IP
  3. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    well i looked over the search engine problem and it actually just returns double of all values in a database.
     
    dmanto2two, Jan 23, 2010 IP
  4. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #4
    Use this:

    
    <?php
    function display_photos ()
    {
       $valid_user = mysql_escape_string($_SESSION['valid_user']);
       mysql_connect("-------","-------","----"); 
       mysql_select_db("-----") or die("Unable to select database");
       $conn = db_connect();
    
       $result = mysql_query("select photo from photo where email='".$valid_user."' ");
    ?>
       <table>
    <?php
       while($row = mysql_fetch_row($result))
          {
    ?>
    
          <tr><td> <img src="images/<?php echo $row[0]; ?>" alt="<?php echo $row[0]; ?>" /> </td></tr>
    
    <?php
          }
    ?>
       </table>
    }
    ?>
    
    PHP:
     
    Silver89, Jan 23, 2010 IP
  5. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    that did it. Thanks man. did the mysql escape string fix that or the while? i was thinking that could patch up my search problem.
     
    dmanto2two, Jan 23, 2010 IP
    Silver89 likes this.
  6. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #6
    The problem was something to do with the for each part I think, might have not been returning the values as you expected. I always keep my php like that, just seems simple to me.

    I added the escape for security :)
     
    Silver89, Jan 23, 2010 IP
  7. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ahh thanks. your a hero
     
    dmanto2two, Jan 24, 2010 IP