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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts

    Convert table rows to individual arrays

    Hello. I am still learning php & mysql. I am trying to pull data from a table for a sub shop. The table is called "subs". Each row in the table has a "name"(of a sub). I know how to use a loop and print out all of the rows, but would rather (gets complicated) like to pull each row, and save it as an array, pull the next row, save it as an array .. and so on, so I can refer to them later. I don't care what the names of the row arrays will be, but do not want to keep having to repeat the code that I have provided. I would greatly appreciate any advice. Thank you in advance, Buffmin.

    PHP Code:
    <?php         
            
    // get results from database 
            
    $result mysql_query("SELECT * FROM subs Where NAME='Ham'")  
                    or die(
    mysql_error());  
                    
        
    $Ham mysql_fetch_row($result);

    echo 
    $Ham[0];
    echo 
    $Ham[1]; 
    echo 
    $Ham[2]; 
    echo 
    $Ham[3]; 

    $result mysql_query("SELECT * FROM subs Where NAME='Turkey'")  
                    or die(
    mysql_error());  
                    
        
    $Turkey mysql_fetch_row($result);

    echo 
    $Turkey[0]; 
    echo 
    $Turkey[1]; 
    echo 
    $Turkey[2]; 
    echo 
    $Turkey[3];
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Fetch_row's purpose is to pull a single row out of a resultset and store it as an array.
    Pulling and printing is pretty much the same as printing them:
    PHP Code:
    $aSubs = array();
    while (
    $row mysql_fetch_assoc($result)) // Changed to assoc so you can target by keyname
    {
        
    $aSubs[] = $row;
    }
    // Or, you can do it on one line too, but I prefer the previous since its more readable.
    while (($aSubs[] = mysql_fetch_assoc($result)) || array_pop($aSubs)); 
    You can also name your subs if you stick with straight associatives:
    PHP Code:
    while ($row mysql_fetch_assoc($result))
    {
        
    $aSubs[$row['Name']] = $row;

    So you can identify a sub under $aSubs['Ham']; for example.

  • Users who have thanked Fou-Lu for this post:

    Buffmin (09-07-2011)

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Ah, great! I now understand why you would use a fetch, and I will try implementing one of those methods now. I like the associative method. Excellent! I appreciate your time and knowledge very much. Buffmin


  •  

    Posting Permissions

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