Jump to content


Photo

Inserting array values in table


  • Please log in to reply
2 replies to this topic

#1 ITStudent

ITStudent
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationGalway, Ireland

Posted 13 February 2006 - 07:43 PM

Is it possible to insert the values of an array into a table, with each value being stored in a new row.

I need to take the value of the array select information from a table based on this and then insert to the second table.

Thanks.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 13 February 2006 - 07:55 PM

[!--quoteo(post=345456:date=Feb 13 2006, 02:43 PM:name=ITStudent)--][div class=\'quotetop\']QUOTE(ITStudent @ Feb 13 2006, 02:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Is it possible to insert the values of an array into a table, with each value being stored in a new row.

I need to take the value of the array select information from a table based on this and then insert to the second table.

Thanks.
[/quote]

i'm not sure i follow what you're asking. the simple answer to your question is yes, you can store each value in an array in a new row. something like this is all you need:
<?php
$q = "INSERT INTO tableName (colName) VALUES ";
foreach ($array as $value)
  $q .= "('$value'), ";

$q = substr($q, 0, strlen($q) - 2);
mysql_query($q);
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 14 February 2006 - 04:06 AM

You can do it all in MySQL with the INSERT INTO SELECT syntax. It should look something like this:

INSERT INTO secondtable (col1, col2, col3)
SELECT col1, col2, col3 FROM firsttable WHERE thepopeiscatholic=1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users