English 中文(简体)
• 连接MySQL的另一个数据库,同时连接 lo
原标题:Connecting Another Database in MySQL while loop
  • 时间:2011-11-22 06:59:47
  •  标签:
  • php
  • mysql

I m试图将数据库数据复制到另一个数据库。 我试图采用以下程序,但该程序并未奏效。 I m New to MySQL and PHP

<?php 
require_once( conf.php );
?>
<?php 
$q=mysql_query("SELECT * FROM navroop_mlm.".memberlogtbl." WHERE totalleft >= 7  AND totalright >= 7 ");
while($r=mysql_fetch_array($q)){
$id=$r[ id ];
$qlnk=mysql_pconnect("localhost", "navroop_mlm", "guwahati0011*");
mysql_select_db("navroop_aip", $qlnk);

mysql_query("INSERT INTO member_login (id, name, username, password,sex) VALUSE ( $id ,  ".$r[ name ]." ,  ".$r[ username ]." ,  ".$r[ password ]." ,  ".$r[ sex ]." )");
}

请帮助我。 这里的用户名、密码和服务器与两个数据库相同。

问题回答

从来没有这样做。 <>不是你认为它会做些什么,将来会造成一定的痛苦。 如果需要复制两个数据库的数据,使用数据库复制或找到另一种解决办法。 Don t Violations of the SPOT Rule

Uhm,

如果两个数据库的连接参数相同,你只能利用MySQL的辛迪加将数据库名称列入询问:

INSERT INTO database2.member_login (id, name, username, password, sex)
SELECT id, name, username, password, sex from navroop_mlm.member_login;

这正是你想要的,即在一个数据库中选择,插入另一个数据库。

问题有可帮助你的答案。

From the answer: “......

$dbh1 = mysql_connect($hostname, $username, $password); 
$dbh2 = mysql_connect($hostname, $username, $password, true); 

mysql_select_db( database1 , $dbh1);
mysql_select_db( database2 , $dbh2);

Then to query database 1, do

mysql_query( select * from tablename , $dbh1);

表2

mysql_query( select * from tablename , $dbh2);

“......

我怀疑,在您的休息之前,你可以与数据库连接,然后在休息室使用第二个链接和您的INSERT INTO查询。

Furthermore, a superior solution would be to use PDO, which is a better alternative to the standard mysql_ functions, as well as more easily allow you to query a second database while looping through the first query s results. http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.3 (a basic PDO tutorial).

您正在铺设通道,而不是关闭。 如上文所述,这可能导致超高射电机的连接门槛。

相反,你可以有单独的联系。

$conn1=mysql_connect("localhost", "navroop_mlm", "guwahati0011*");
$conn2=mysql_connect("localhost", "navroop_mlm", "guwahati0011*");
mysql_connect_db("navroop_mlm",$conn1);
mysql_connect_db("database2",$conn2);

$q=mysql_query("SELECT * FROM $memberlogtbl WHERE totalleft >= 7  AND totalright >= 7 ",$conn1);

while($r=mysql_fetch_array($q))
{
$id=$r[ id ];

mysql_query("INSERT INTO member_login (id, name, username, password,sex) VALUES ( $id ,  ".$r[ name ]." ,  ".$r[ username ]." ,  ".$r[ password ]." ,  ".$r[ sex ]." )",$conn2);
}

mysql_close($conn1);
mysql_close($conn2);




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签