Jump to content

Insert data from one table to another


brooksh

Recommended Posts

First of all, I want to know if this script will even work. I only want it inserted if it doesn't already exist. I know that I could do replace, but I think it would use more of the servers resources. This is about 8000 rows from the first table. And if it is possible to make it more efficient.

 

	
$sql = "select id,name from names";
        $result = mysql_query ($sql);
            while($data = mysql_fetch_array ($result, MYSQL_ASSOC))
        {
$newid = $data[id];
$newname = $data[name];
mysql_query("INSERT into newnames SET id=$newid, name=$newname WHERE id > $newid");
}

Link to comment
https://forums.phpfreaks.com/topic/42393-insert-data-from-one-table-to-another/
Share on other sites

I have trouble reading other people's code, I don't know why.. it's just the way I have always been. Though I would like to try and explain how I would get this accomplished, and if you think you can do that go for it.

 

First you would want to grab all information from the first one, then run a while for that query, in the while you should tell it to check the other table for the information given, if it exists do nothing, if it doesn't.. then place the information in (use an if for that). Since you're running it in a while, it should go through and do this for every line untill it is finished.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.