fer0an Posted February 20, 2010 Share Posted February 20, 2010 hello I've 2 db, db1 and db2. db1 have some data, I want fetch "id" and "comments" from this db then insert this id and comment to db2. anyone can help me? thank you Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/ Share on other sites More sharing options...
fer0an Posted February 20, 2010 Author Share Posted February 20, 2010 anyone can help me? :'( Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015164 Share on other sites More sharing options...
jskywalker Posted February 20, 2010 Share Posted February 20, 2010 INSERT INTO db2.table SELECT id, comment from db1.table http://dev.mysql.com/doc/refman/5.1/en/ansi-diff-select-into-table.html Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015168 Share on other sites More sharing options...
jl5501 Posted February 20, 2010 Share Posted February 20, 2010 you cannot copy data from 1 database to another in the same query. You would need to extract your data from the first database, on one connection, then insert into the other, on another connection Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015175 Share on other sites More sharing options...
jskywalker Posted February 20, 2010 Share Posted February 20, 2010 you cannot copy data from 1 database to another in the same query. and example that this is possible on MySQL is given on this page: http://dev.mysql.com/doc/refman/5.0/en/use.html Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015177 Share on other sites More sharing options...
jl5501 Posted February 20, 2010 Share Posted February 20, 2010 different tables, yes, not different databases Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015181 Share on other sites More sharing options...
fer0an Posted February 20, 2010 Author Share Posted February 20, 2010 diffrent database anyone can help? Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015186 Share on other sites More sharing options...
jskywalker Posted February 20, 2010 Share Posted February 20, 2010 The following example accesses the author table from the db1 database and the editor table from the db2 database: its IS talking about two database's....!!! and fer0an, did you check the link? Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015187 Share on other sites More sharing options...
jl5501 Posted February 20, 2010 Share Posted February 20, 2010 When copying from one database to another, you have to extract first, then switch connections with 'use' then insert into the other. Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015190 Share on other sites More sharing options...
jskywalker Posted February 20, 2010 Share Posted February 20, 2010 When copying from one database to another, you have to extract first, then switch connections with 'use' then insert into the other Only if the two databases are on different servers (But you might also be so polite to react on what another person is saying, without repeating yourself...) Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015193 Share on other sites More sharing options...
fer0an Posted February 20, 2010 Author Share Posted February 20, 2010 I've fetch my data with this code : [color=red] $query = mysql_query("SELECT * FROM jos_mxc_admcomments") or die(mysql_error()); $row = mysql_fetch_array( $query ); $id= $row['contentid']; $comment = $row['comment']; while ($row = mysql_fetch_array( $query )) { echo $row['contentid']; echo $row['comment']; } mysql_close($con);[/color] then I want insert into db2 : [color=red] //connecting db2 $con1 = mysql_connect($dbhost1, $dbuser1, $dbpass1); mysql_select_db($dbname1) or die('Could not select database'); { $insert1 = "INSERT INTO `jos_jcomments` ( `id` , `parent` , `object_id` , `object_group` , `object_params` , `lang` , `userid` , `name` , `username` , `email` , `homepage` , `title` , `comment` , `ip` , `date` , `isgood` , `ispoor` , `published` , `subscribe` , `source` , `checked_out` , `checked_out_time` , `editor` ) VALUES ( NULL , '0', '".$id1."', 'com_content', '', '', '0', '', '', '', '', '', '".$title."', '', '".date('Y-m-d G:i:s')."', '0', '0', '1', '0', '', '0', '".date('Y-m-d G:i:s')."', '' )"; if(!mysql_query($insert1,$con1)) { die('Error:2vomi'. mysql_error() ); } } [/color] anyone can help me about this codes? Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015195 Share on other sites More sharing options...
jskywalker Posted February 20, 2010 Share Posted February 20, 2010 I've fetch my data with this code : anyone can help me about this codes? no, sorry, i can not, because you dont ask a question about this code, and you do NOT state what is going wrong, or what it supposes to do.... Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015197 Share on other sites More sharing options...
fer0an Posted February 20, 2010 Author Share Posted February 20, 2010 jskywalker can you help me to generate a code ? Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015198 Share on other sites More sharing options...
jskywalker Posted February 20, 2010 Share Posted February 20, 2010 can you describe what is wrong with the two pieces of code you have? can you describe what the code should do? what errors are you getting now? are these two databases on 1 databasehost, or on 2 diferent host? Link to comment https://forums.phpfreaks.com/topic/192704-fetch-and-insert-into-two-db/#findComment-1015245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.