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 Quote Link to comment Share on other sites More sharing options...
fer0an Posted February 20, 2010 Author Share Posted February 20, 2010 anyone can help me? :'( Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 20, 2010 Share Posted February 20, 2010 different tables, yes, not different databases Quote Link to comment Share on other sites More sharing options...
fer0an Posted February 20, 2010 Author Share Posted February 20, 2010 diffrent database anyone can help? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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...) Quote Link to comment 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? Quote Link to comment 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.... Quote Link to comment 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 ? Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.