我有2个链接在一起的数据库。我需要从该表中检索数据,然后根据其唯一ID号将这些列插入其他数据库的表中。
<?php
$handle = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_query("USE shop",$handle);
$query = "SELECT ModelCode,Class FROM shopfloor_pro WHERE CommNo = '0985560712'";
$result = mysql_query($query);
while ($data = mysql_fetch_object($result)){
$variable1 = $data->ModelCode;
$variable2 = $data->Class;
mysql_query("USE vt",$handle);
$sql = "INSERT INTO track SET
t_model_code = '$variable1',
t_class = '$variable2' WHERE t_comm_no = '0985560712'";
if (!mysql_query($sql)) {
echo '<p>Error adding data into database: ' . mysql_error() . '</p>';
}
mysql_query("USE paintshop",$handle);
}
?>
this is the data that i want to retrieve
this is where i want to put the data
运行代码时显示 “在向数据库中添加数据时出错:您的SQL语法有错误;请在与MySQL服务器版本相对应的手册中找到在第3行'WHERE t_comm_no ='0985560712”附近使用的正确语法”
答案 0 :(得分:0)
您最有可能在单个查询中执行此操作-但要指出,mysql
api很久以前已被弃用,并已从PHP 7+中完全删除。
要在单个操作中进行查询,您可以尝试如下操作:
insert into `vt`.`track` (`t_model_code`,`t_class` )
select `ModelCode`,`Class` from `shop`.`shopfloor_pro` where `CommNo`='0985560712'