简体   繁体   中英

Fetch array and Insert Into different database. Increment variable in Insert string?

I have tried several different methods of implementation for this and can't seem to get the last little bit correct. The most efficient is obviously the best scenario but at this point it's second to getting it working.

I'm attempting to pull data from the Syspro connection and inserting it into the MySQL connection. I don't need to print anything with this code, just simply insert into the MySQL database.

//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);

Well, I would do this way:

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

No need to loop again (as you previously tried :) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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