• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop problem

#1
[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
Code:
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
Code:
$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..

#2
[eluser]bgreene[/eluser]
in mysql 5 you can use "insert into city_areas2 ... on duplicate update". it keeps your code a bit neater

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.