dscrb00804 2013-05-02 13:51
浏览 135
已采纳

使用php将mysql列与SQL Server进行比较

I have two databases, one online (mysql) and one in my office (SQL Server) which I would like to compare and update where a value is different.

I am using php to connect to the SQL Server database and run a query to retrieve the information, then connecting to the Mysql database running a query. Then I need to compare the two queries and update where necessary.

Is there somewhere I can look for tips on how to do this, I am sketchy on PHP and struggling really.

This is as far as I have got-:

<?php
$Server = "**server**";
$User = "**user**";
$Pass = "**password**";
$DB = "**DB**";

//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server"); 

//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't open database $DB"); 

//declare the SQL statement that will query the database
$query = "SELECT p.id, p.code, ps.onhand";
$query .= "FROM products p with(nolock)";
$query .= "INNER JOIN productstockonhanditems ps with(nolock)";
$query .= "ON ps.ProductID = p.ID";
$query .= "WHERE ps.StockLocationID = 1"; 

//execute the SQL query and return records
$get_offlineproduct2 = mssql_query($query);

mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error()); 
$get_onlineproducts = mysql_query(SELECT w.ob_sku, w.quantity
FROM product_option_value AS w
ORDER BY ob_sku) 
or die(mysql_error());

//close the connection
mssql_close($dbhandle);

?>

I am looking to compare the value p.code to w.ob_sku and whenever they match copy the value of ps.onhand to w.quantity so the online database has the correct quantities from the office database.

My question I guess is how close am I to getting this right? Also am I doing this the right way, I don't want to get so far and realise that i am just wasting my time...

Thanks!

  • 写回答

1条回答 默认 最新

  • dongqian9013 2013-05-02 15:39
    关注

    You do not need to fetch any record from MySQL, since you actually want to update it.

    I would do something like this:

    $query = 'SELECT p.code, ps.onhand FROM (...)';
    
    // execute the SQL query and return a result set
    // mssql_query() actually returns a resource
    // that you must iterate with (e.g.) mssql_fetch_array()
    $mssqlResult = mssql_query($query); 
    
    // connect to the MySQL database
    mysql_connect("**Host**", "**username**", "**password**") or die(mysql_error());
    mysql_select_db("Database_Name") or die(mysql_error());
    
    while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
        $mssqlCode = $mssqlRow['code'];
        $mssqlOnHand = $mssqlRow['onhand'];
        mysql_query(
            "UPDATE product_option_value SET quantity = $mssqlOnHand WHERE ob_sku = $mssqlCode"
           // extra quotes may be required around $mssqlCode depending on the column type
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作