Php脚本从其他数据库更新VIEW

标签 php mysql database

我需要编写一个脚本,该脚本将从两列中获取值,并使用它们来更新我在另一个数据库中创建的 View 中的列。在第一个数据库中,我有 sku 和 qty 以及 View 中的信息。

这是我的代码:

$server = 'localhost';
$user = 'invodata';
$pass = 'Abcd1234!1';
$dbname = 'tboinvodata';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("tboinvodata") or die(mysql_error());

 $result = mysql_query("SELECT item, onhand FROM immaster"); <- this is getting the values from     the columns in the first data base

 $server = 'localhost';<-setting up my second connection to other database
 $user = 'tbo';
 $pass = 'Abcd1234!1';
 $dbname = 'i187358_mage1';
 $con = mysql_connect($server, $user, $pass) or die("Can't connect");
 mysql_select_db("i187358_mage1") or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {<-this gets the array from other database

UPDATE qtyview SET qty = $row["onhand"] WHERE sku = item;<- this should update the necessary columns "sku" is used in my view and "item" is used in the first data base I use this so the proper rows in the other columns get updated. 
}

?>

真的不确定我做错了什么,但我对此还很陌生。

最佳答案

您可以多次调用 mysql_connect() 并像这样使用它们。

首先连接到两个MYSQL USER

$con1 = mysql_connect($server, $user, $pass); 
$con2 = mysql_connect($server, $user, $pass, true);

然后与不同的DATABASE建立连接

mysql_select_db('firstdatabase', $con1);
mysql_select_db('seconddatabase', $con2);

然后像这样从firstdatabase查询

mysql_query('select * from views', $con1);

并从第二个数据库查询

mysql_query('select * from views', $con2);

此代码未经我测试...但我认为它对您很有用..:)

关于Php脚本从其他数据库更新VIEW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596636/

相关文章:

mysql - 为 MySQL 客户端的结果集输出中的数字列字段指定除 10 之外的默认基数?

mysql - 高写表上的索引不好?

mysql - 如何找到复杂的SQL查询

android - 如何使用 Sqlite 将一个应用程序的数据库访问到另一个应用程序?

PHP 多个复选框

php - 类型提示数组的内容

php - Metabox 选择数组在自定义列中显示帖子 ID 而不是帖子名称

php - 从字段以特定字符/数字结尾的 MySQL 表中进行选择的任何方法?

Javascript 也更新了页面

sql - 带 Hibernate 的组合框