SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question [SOLVED] Looping over a mysql column and turning the results into an array

    Hi all, I've hit a brick wall and been banging my head off of it all day. Just one of those mental blocks I guess.
    Anyway, I'm trying to do exactly as described in the title. Here's my code so far..

    PHP Code:
    // Get the page file names from database 
    function page_file_names()
      {
          
    $connection db_connect();            
        
    $query = ("SELECT page_file FROM pages");             
        
    $result mysql_query($query);        
        while(
    $row mysql_fetch_array($result)){            
            
    $row['page_file'];// How can I turn this bit into an array?
            
    }
       } 
    Now, if I did as below, great I get a nice list of all the values from that column, but it I try return $row['page_file']; it only gives back the first iteration.
    PHP Code:
    while($row mysql_fetch_array($result)){            
            echo 
    $row['page_file'].'<br />';
            } 

    EDIT: Doh! I just sussed it out after finally posting for help. I knew that would happen! :P

    PHP Code:
    // Get the page file names from database 
    function page_file_names()
      {
          
    $connection db_connect();            
        
    $query = ("SELECT page_file FROM pages");             
        
    $result mysql_query($query);    
        
    $column_array = array();    
        while(
    $row mysql_fetch_array($result)){            
            
    array_push($column_array$row['page_file']);
            
            }
            return 
    $column_array;
       } 
    Last edited by BuddyH; Sep 28, 2011 at 15:23. Reason: Sussed it!

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    698
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    $column_array[] = $row['page_file'];
    Also works. Bit less typing.


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
  •