Student
Student

Reputation: 531

I want to do an SQL query for every member in the members table

How do I do that?

I have a table with,at the moment, 3 members. For each one of these members I want to add a row to another table. Should I use a while inside a while? Or a Foreach loop?

This is what I have so far.

$sql = "SELECT email FROM members";
$query = mysql_query($sql);

while ($row = mysql_fetch_assoc($query)){

// Do I add a new while in here? Do I use a for loop?

}

Upvotes: 2

Views: 475

Answers (3)

sakhunzai
sakhunzai

Reputation: 14470

based on your table info :

while ($row = mysql_fetch_assoc($query)){

 mysql_query("insert into aanwezigheid set email='{$row['email']}', 
   date='{$your_date_var}',
   city='{$your_city_var}',
   address='{$your_address_var}'" );
}

Upvotes: 0

kba
kba

Reputation: 19466

This can be done with just a single query.

INSERT INTO anotherTable (email) SELECT email FROM members; 

You can read more about the INSERT ... SELECT syntax in the MySQL documentation.

Upvotes: 1

Andomar
Andomar

Reputation: 238086

You could use an insert statement to add a set of rows to another table:

insert  OtherTable
        (email)
select  email
from    Members

Upvotes: 1

Related Questions