我需要将一些行从数据库复制到另一个数据库。我遇到困难。我发现了几种方法,但似乎没有一种有效。我正在使用的php版本是5.4。
两个连接都在同一台服务器上,但是其他所有内容都不同
这是我发现的php代码,它似乎根本不起作用,我无法从第一个数据库中选择
// Create connection
$wpdb = mysql_connect($servername, $username, $password);
// Check connection
if ($wpdb->connect_error) {
die("Connection failed: " . $wpdb->connect_error);
}
echo "Connected local successfully\n";
//$starttime = date("h:i:sa");
$mydb = mysql_connect('localhost','dbname','dbpassword', true);
// Check connection
if ($mydb->connect_error) {
die("Connection failed: " . $mydb->connect_error);
}
echo "Connected to Integrity successfully\n";
mysql_select_db($database, $wpdb);
mysql_select_db('wordpress_0', $mydb);
答案 0 :(得分:0)
您可以尝试使用PDO。PDO提供了与许多不同数据库进行通信的通用接口。
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user',
'password');
答案 1 :(得分:0)
我拒绝提供对mysql_
语法的支持,所以我将提供升级版本。
几乎完全从php手册中复制... http://php.net/manual/en/mysqli.select-db.php
代码:
/* attempt and check connection including first database selection "test" */
if (!$mysqli = new mysqli("localhost", "root", "", "test")) {
// never show the actual error to the public
echo "<div>Database Connection Error: " , $conn->connect_error , "</div>";
exit();
}
/* return name of current default database */
if (!$result = $mysqli->query("SELECT DATABASE()")) {
// never show the actual error to the public
echo "<div>Syntax Error: " , $conn->error , "</div>";
} else {
echo "<div>Default database is: " , $result->fetch_row()[0] , "</div>";
$result->close();
}
/* change db to "mysql" db */
$mysqli->select_db("mysql");
/* return name of current default database */
if (!$result = $mysqli->query("SELECT DATABASE()")) {
// never show the actual error to the public
echo "<div>Syntax Error: " , $conn->error , "</div>";
} else {
echo "<div>Default database is: " , $result->fetch_row()[0] , "</div>";
$result->close();
}
$mysqli->close();
输出:
Default database is: test
Default database is: mysql