Page 1 of 1
code required to insert whole data of 1 table into 2nd
Posted: Tue Jan 05, 2010 9:38 am
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);
Re: code required to insert whole data of 1 table into 2nd
Posted: Tue Jan 05, 2010 10:34 am
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.
Re: code required to insert whole data of 1 table into 2nd
Posted: Tue Jan 05, 2010 10:41 am
by rhecker
For mySQL:
INSERT INTO table_2 (column1, column2) SELECT column1, column2 FROM table_1
And make sure the data types match
Re: code required to insert whole data of 1 table into 2nd
Posted: Thu Jan 07, 2010 2:14 am
by thefarhan
Thank you -Shawn , You have solved my problem.
Re: code required to insert whole data of 1 table into 2nd
Posted: Thu Jan 07, 2010 2:47 am
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.
Re: code required to insert whole data of 1 table into 2nd
Posted: Thu Jan 07, 2010 7:25 am
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.