0

我为此尝试了几种不同的实现方法,但似乎无法让最后一点正确。最有效的显然是最好的方案,但在这一点上,它的工作是次要的。

我正在尝试从 Syspro 连接中提取数据并将其插入 MySQL 连接。我不需要使用此代码打印任何内容,只需插入 MySQL 数据库即可。

//MySQL Connection
$mysqlcon=mysqli_connect("localhost","root","","production");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

//Syspro connection
$conn=odbc_connect('syspro','','');
if (!$conn)
  {exit("Connection Failed: " . odbc_error());}

//Warehouse valuation for S6/SX/SN/SW
$sql_warehouses=
    "SELECT SysproCompanyJ.dbo.InvWarehouse.Warehouse, SUM(UnitCost * QtyOnHand) AS 'Value'

    FROM SysproCompanyJ.dbo.InvWarehouse         

    WHERE   SysproCompanyJ.dbo.InvWarehouse.QtyOnHand > '0' 
        AND SysproCompanyJ.dbo.InvWarehouse.Warehouse NOT LIKE 'A_'
        AND SysproCompanyJ.dbo.InvWarehouse.Warehouse = 'S6' 
        OR Warehouse = 'SX'
        OR Warehouse = 'SN' 
        OR Warehouse = 'SW'

    GROUP BY Warehouse";

$test = array();
$i=0;   
$rs=odbc_exec($conn,$sql_warehouses);
if (!$rs)
  {exit("Error in SQL");}

    while ($row = odbc_fetch_array($rs))    {
        //echo $row['Warehouse'] . ':' .$row['Value']; 
        array_push($test,$row['Warehouse'],$row['Value']);
        //echo mysqli_query($mysqlcon,"INSERT INTO inv_valuation (warehouse, value)
        //VALUES ({$row['Warehouse']} ,{$row['Value']})");
        }

for($i = 0, $size = count($test); $i < $size; ++$i) {
    //echo "WH=" . $test[$i] . ", $=" . $test[++$i];
    mysqli_query($mysqlcon,"INSERT INTO inv_valuation (warehouse, value)
    VALUES ($test[$i] ,$test[++$i])");
    }

    //print_r($test);
    //var_dump($test);
    //echo count($test);
4

1 回答 1

1

好吧,我会这样做:

while ($row = odbc_fetch_array($rs))    {
    mysqli_query($mysqlcon,"INSERT INTO inv_valuation (warehouse, value)
        VALUES ('" . $row['Warehouse'] . "', '" . $row['Value'] . "')");
}

无需再次循环(正如您之前尝试过的:))

于 2013-11-05T15:20:11.067 回答