我需要编写一个脚本,该脚本将从两列中获取值,并使用它们来更新我在另一个数据库中创建的 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/