CodeIgniter Forums

Full Version: loop problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Bigil Michael[/eluser]
i would like to copy a table from one database to another database. in order to do that first i test it as a local project.

here 2 tables are present they are given below
table name city_areas
id  city_id  name
1  1       Jayanagar
2  1       R T Nagar
3  1       Indira Nagar
4  1       Vijaya Nagar
second table name is city_areas2.

I want to copy the datas from cityareas to cityareas2. if the same row present i want to do update else i want to do insert.
I have developed the queries like this
$result_city_areas = mysql_query("SELECT * FROM city_areas ");
$bangalore_city_areas = mysql_query("SELECT * FROM city_areas2 ");
while($bangalore_row = mysql_fetch_array($bangalore_city_areas))
  while($row = mysql_fetch_array($result_city_areas))
   if($row[id] != $bangalore_row[id]) {
    mysql_query("INSERT INTO city_areas2 (id,city_id, name) values ('$row[id]','$row[city_id]','$row[name]')");
   } else {
    mysql_query("UPDATE city_areas2 SET name = '$row[name]' WHERE id = '$row[id]' ");

Now it is working but the result is not correct.

can anyone help me to solve this problem????

thanks in advance..

El Forum

in mysql 5 you can use "insert into city_areas2 ... on duplicate update". it keeps your code a bit neater

El Forum

[eluser]Bigil Michael[/eluser]
thanks for your reply