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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    print the results of a query in a foreach loop?

    Hi, say I have a query like so:

    PHP Code:

    foreach($_REQUEST['r'] as $position => $row
        
      {   
    $row implode("', '",$row);
          
    $result mysql_query("Insert into player_stats (position,shirt_number,player_id,goals,cards,substituted,used_sub,injury,season,report_id)   
                                 values('$position','$row','$currentSeason','$report_id')"
    );
      } 
    How do I print the results of that query to my Browser for each time?

    Thanks

  • #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
    Just print out $result within it. I'm not sure I'd take this approach though, the overhead is high. Insert will allow multiple values to be inserted at the same time:
    Code:
    INSERT INTO table VALUES
    (column1, column2, column3),
    (column1_2, column2_2, column3_2), 
    (...)
    This can be used to dynamically build the relevant code. You can retrieve the number of inserted values with mysql_affected_rows.

    I can't give you anything for your current though, the SQL itself is invalid. You have more columns listed than corresponding values, so your actual query will fail (use or die to see this eg: mysql_query($yourquery) or die(mysql_error());). You'll be wanting to use mysql_real_escape_string on anything user provided from your $_REQUEST, but if possible I'd recommend avoiding $_REQUEST (since its final override is cookie), and retrieve from the appropriate $_GET or $_POST instead.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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