Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2010, 07:08 PM   PM User | #1
stfc_boy
Regular Coder

 
Join Date: Jun 2007
Posts: 310
Thanks: 86
Thanked 3 Times in 3 Posts
stfc_boy is an unknown quantity at this point
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
stfc_boy is offline   Reply With Quote
Old 08-14-2010, 08:34 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:53 PM.


Advertisement
Log in to turn off these ads.