简体   繁体   English

Cron php更新/从另一个插入一个sql表

[英]Cron php update/insert one sql table from another

I am not that good in php so I would appreciate your help. 我在php中不是那么好,所以我很感激你的帮助。

I need to update or insert certain data from one mysql table to another mysql table same server! 我需要更新或插入一个mysql表中的某些数据到另一个mysql表相同的服务器!

Bought tables have same column and what I want is to update certain rows with prechosen mid! 买的表有相同的列,我想要的是用prechosen mid更新某些行! Table: 表:

|ID| |mid| |value1| |value2|

I did the part where php for update or insert to check if it exists but what I need it to do is to repeat the step pos each given 我做了更新或插入php的部分,以检查它是否存在但我需要它做的是重复每个给定的步骤pos

So what I need it to repeat this script for each mid I list to be done 所以我需要它为每个中间列表重复这个脚本来完成

mid1 = 66
mid2 = 78
mid3 = 24


$con = mysql_connect("localhost","User","Pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db(baza, $con);

$result = mysql_query("SELECT * FROM table WHERE mid='???' ");

while($row = mysql_fetch_array($result))
{
$mid=$row['mid'];
$name=$row['value1'];
$alias=$row['value2'];
}

mysql_close($con);
?> 
<?php
$con2 = mysql_connect("localhost","user2","pass2");
if (!$con2)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db(baza2, $con2);


$query = "SELECT * FROM table WHERE mid='$mid' ";
$resulta = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($resulta) )
{

mysql_query("UPDATE table SET mid='$mid', name='$name', alias='$alias'");
mysql_close($con2);
echo "1 record added1";
}
else
{
$sql="INSERT INTO table (mid, name, alias)
VALUES
('$mid','$name','$alias')";

if (!mysql_query($sql,$con2))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con2);
}

Thank you in advance! 先感谢您!

If the 2 databases are on the same server, you can set-up your account to have access to both databases. 如果2个数据库位于同一服务器上,则可以设置帐户以访问这两个数据库。 If so, you can simple select the database in your queries on this way: 如果是这样,您可以通过以下方式在查询中简单地选择数据库:

mysql_query("SELECT * FROM my_database.my_table WHERE ..");

Your code will be.. 你的代码将是......

$mid_array = array(66, 78, 24);
$con = mysql_connect("localhost","user","pass");
if(!$con) {
    die('could not connect: '.mysql_error());
}
mysql_select_db(baza, $con);

foreach($mid_array AS $mid) {
    $result = mysql_query("SELECT * FROM table WHERE mid = '".$mid."'";
    while($row = mysql_fetch_array($result)) {
       mysql_query("INSERT INTO baza2.table (mid, name, alias) VALUES ('".$row['mid']."', '".$row['name']."', '".$row['alias']."') ON DUPLICATE KEY UPDATE name = '".$row['name']."', alias = '".$row['alias']."'");
    }
}
mysql_close($con);

You don't need a different query to check if the record with mid = .. already exists. 您不需要使用其他查询来检查mid = ..的记录是否已存在。 You can simply handle this with the mysql ON DUPLICATE KEY UPDATE statement which will do this for you in only 1 query! 您只需使用mysql ON DUPLICATE KEY UPDATE语句处理此问题,该语句将仅在1个查询中为您执行此操作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM