[英]Can I create a table from another table that I have in a $var in PHP
As seen below, I' m taking some data in a variable from a table and I' m trying to create a new table in another database (of course in the same host). 如下所示,我正在从表的变量中获取一些数据,并且试图在另一个数据库(当然是在同一主机中)中创建一个新表。 The table is never created and I dont'even know if it's possible.
该表永远不会创建,我什至不知道是否可能。
Please note that I can connect only with user1 to db1 and with user2 to db2, so that's why I try this stuff. 请注意,我只能将user1连接到db1,将user2连接到db2,因此这就是我尝试使用该工具的原因。
Any help? 有什么帮助吗?
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
You code does not make sense, as I have no idea what $result_new will contain. 您的代码没有意义,因为我不知道$ result_new将包含什么。 The create table command requires a certain syntax.
create table命令需要某种语法。
However, I can answer your question : 但是,我可以回答您的问题:
The table is never created and I dont'even know if it's possible.
该表永远不会创建,我什至不知道是否可能。
The reason why you don't know if you are not properly querying the error. 您不知道是否未正确查询错误的原因。 Use the mysql_error() function to do this.
使用mysql_error()函数执行此操作。
if($result){
echo "Table newtable Created";
echo "<BR>";
}
else {
echo "ERROR Creating Table newtable : ";
echo mysql_error();
echo "<BR>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.