开发者

magento make multiple database connections without losing array values

开发者 https://www.devze.com 2023-03-23 07:32 出处:网络
I am trying to make connections to two different databases so my script should work as follows Find all orders for the current logged in customer where the order state is complete, it is a virtual

I am trying to make connections to two different databases so my script should work as follows

  1. Find all orders for the current logged in customer where the order state is complete, it is a virtual product and it has a juno order id (this query works fine)

  2. Collect all of order ids that have been found and store them in an array (this works fine)

  3. now connect to sales_order_items and for each item that is part of an order id check to see if the database has a url download link,

  4. if not i will connect to an api --

the problem is when i want to make my second connection i seem to lose all the values that are stored in the $orderIds array.

i have been looking for solutions but i am pretty new to the zend framework开发者_如何学C

any help would be much appreciated

my script is as follows

        $conn = Mage::getSingleton('core/resource')->getConnection('core_write');
    $result = $conn->query('select * from sales_flat_order WHERE customer_id='.$session->getCustomerId().' AND state="complete" AND is_virtual=1 AND juno_order_id!="null"');

    $orderIds=array();

    foreach ($result as $orderId)
    {
    $orderIds[]=$orderId[entity_id];    

    $itemsonOrder=$conn->query('select * from sales_flat_order_items WHERE order_id='.$order[entity_id]);



    }



// value of first array $orderIds gets lost if i make annother connection using $conn
        echo 'items on order';
        print_r($itemsonOrder);
        echo '<pre>';
        print_r($orderIds);
        echo '</pre>';


Well you aren't done with the first query yet when you are connecting with the second query. Go ahead and finish with the first query, then start the second one. Try something more like this...

$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$result = $conn->query('select * from sales_flat_order WHERE customer_id='.$session->getCustomerId().' AND state="complete" AND is_virtual=1 AND juno_order_id!="null"');
$orderIds=array();
foreach ($result as $orderId)
{
  $orderIds[]=$orderId[entity_id];    
}
foreach ($orderIds as $orderId)
{
  $itemsonOrder=$conn->query('select * from sales_flat_order_items WHERE order_id='.$orderId);
}

Also, you should probably make sure you are using parameters when doing queries. Concatenating sql strings like that can be dangerous (leaves you open to vulnerabilities sometimes). For example,

$conn->query('select * from sales_flat_order_items WHERE order_id='.$orderId);

should be changed to

$conn->query('select * from sales_flat_order_items WHERE order_id=?', array($orderId));

Also, do you really need to "select *"??? I mean, just select the columns you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消