0
如下所示,我從表格中獲取某個變量的某些數據,並試圖在另一個數據庫中創建一個新表格(當然在同一個主機上)。該表從未被創建,我也不知道是否有可能。
請注意,我只能與user1連接到db1,而user2連接到db2,所以這就是爲什麼我嘗試這個東西。
任何幫助?
mysql_connect("localhost:3036", "user1", "pass1")or die("cannot connect");
mysql_select_db("db1")or die("unable to select db1");// connect to db1 as user1
$takedata="SELECT ID, post_date, post_content, guid
FROM `db1`.`wp_posts`"; // in var $takedata , I save the query
$result_new=mysql_query($takedata); //execute the query
if($result_new){
echo "data from db1 taken sucsessfully";
echo "<BR>";
}
else {
echo "ERROR taking data from db1";
echo "<BR>";
}
mysql_close(); // close connection with db1
mysql_connect("localhost:3036", "user2", "pass2")or die("cannot connect");
mysql_select_db("db2")or die("unable to select db2");// connect to db2 as user2
$sql="CREATE TABLE `db2`.`newtable` AS (`$result_new`)"; //Here is the tricky part
//I'm trying to create table in db2 with the data
//I took from db1.
$result=mysql_query($sql);
if($result){
echo "Table newtable Created";
echo "<BR>";
}
else {
echo "ERROR Creating Table newtable";
echo "<BR>";
}
mysql_close(); // close connection with db2
是的,你可以先得到結果遊標,然後從中創建一個數組。然後創建新表並將數據逐個插入到該表中。 –