SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Foreach using sql database?

    Newbee... i have some code that creates a foreach loop in php which works but how could i replace the names and ages from a sql database?

    I am having some problems finding a good example online.

    Any help appreciated or let me know any good tutorials on this.

    My code is below,

    PHP Code:
    <?
    $names_ages 
    = array ( "bob" => "bob is 20""sam" => "sam is 45");
    foreach (
    $names_ages as $name => $age)
    {
    echo 
    "$name : $age<br>";
    }
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "select name, age from people_table"// Build query

    $results mysql_query$query ); // Execute query

    while ( $row mysql_fetch_assoc$results )) { // Loop through results
        
    echo "Name is " $row['name']; // Echo name field
        
    echo "Age is " .$row['age']; // Echo age field
    }

    unset( 
    $row ); // Free row
    mysql_free_result$query ); // Free resultset 
    This is a short version your production code should check for a valid resultset.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it not possible to do this in a foreach loop?

    Similar to my original code?

    Otherwise i imagine it will be a bit of a headache for me to change this.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "select name, age from people_table"// Build query
    $results mysql_query$query ); // Execute query

    $u_bounds mysql_num_rows$results ); // Get the number of rows
    $row mysql_fetch_assoc$results ); // Get first row

    for ($i 0$i $u_bounds$i++ ) { // Loop through results
        
    echo "Name is " $row['name']; // Echo name field
        
    echo "Age is " .$row['age']; // Echo age field
        
    $row mysql_fetch_assoc$results // Get the next row
    }

    unset( 
    $row ); // Free row
    mysql_free_result$query ); // Free resultset 
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    mysql_free_result( $query ); // Free resultset
    should be:
    mysql_free_result( $results ); // Free resultset
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks logic_earth you are correct, my mistake.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


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
  •