简体   繁体   English

我可以从PHP $ var中的另一个表创建表吗?

[英]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.

相关问题 我有某种php代码,在此我想将连接变量($ conn)发送到另一个页面中,以便我可以动态创建数据库表 - I have sort of php code, in this i want to send connection variable ($conn) into another page so that i can create table for database dynamically 我从php页面在mysql表中插入jQuery var时遇到了一件很奇怪的事情 - I have encountered a really weird thing while Inserting jquery var in mysql table from a php page 如何在php中将一个表中的变量用作另一表的SELECT参数 - How can I use a variable from one table as a SELECT parameter for another table in php 我可以从一个ID =另一个查询的表中选择全部的SQL查询吗? - Can I have an SQL query where I select all from a table where ID = another query? MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? 我可以从现有的sql表创建表定义吗? - Can I create a table definition from an existing sql table? 如何将文本框放在表数据中,哪些行已从另一表追加/移动? - How can I put a textbox inside a table data which rows have been appended/moved from another table? 从表中获取用户,其中group = group另一个表行,但是我有另一个表行的id - Get users from table where group=group of another table row but i have id of other table row 我如何从txt文件到php var使用var - How can i use var from txt file to php var 如何合并来自表单和来自三个mysql表的数据,以使用php将数据插入另一个表中? - How can I combine data from form and from three mysql tables to insert data in another table with php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM