code required to insert whole data of 1 table into 2nd

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thefarhan
Forum Newbie
Posts: 5
Joined: Tue Dec 22, 2009 7:02 am

code required to insert whole data of 1 table into 2nd

Post by thefarhan »

I want to put whole of the data from one table to another. what PHP code is required to do so? right now i m using it, which is not working.

Code: Select all

 
$strsql="Select column_1,Column_2,Column_3 from table_1";
$resultsql=mysql_query($strsql);
$rowresult=mysql_fetch_array($resultsql);
 
$strinsert="insert into table_2 (column_1,Column_2,Column_3) $Values('".$rowresult[column_1]."','".$rowresult[column_2]."','".$rowresult[column_3]."')";
$resultinsert=mysql_query($strinsert);
 
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: code required to insert whole data of 1 table into 2nd

Post by AbraCadaver »

First, you are only returning 1 row from the result set with $rowresult=mysql_fetch_array($resultsql). You need this in a loop and do your inserts in the loop. Second, $Values isn't correct, you need VALUES. There's probably a better way to do this. I'll check.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: code required to insert whole data of 1 table into 2nd

Post by rhecker »

For mySQL:

INSERT INTO table_2 (column1, column2) SELECT column1, column2 FROM table_1

And make sure the data types match
thefarhan
Forum Newbie
Posts: 5
Joined: Tue Dec 22, 2009 7:02 am

Re: code required to insert whole data of 1 table into 2nd

Post by thefarhan »

Thank you -Shawn , You have solved my problem.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code required to insert whole data of 1 table into 2nd

Post by requinix »

thefarhan wrote:Thank you -Shawn , You have solved my problem.
The "better way to do it" that he mentioned is what rhecker posted. Your SELECT+INSERT process right now is a bad idea.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: code required to insert whole data of 1 table into 2nd

Post by AbraCadaver »

tasairis wrote:
thefarhan wrote:Thank you -Shawn , You have solved my problem.
The "better way to do it" that he mentioned is what rhecker posted. Your SELECT+INSERT process right now is a bad idea.
I agree.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply