在循环内运行查询以执行另一个循环

时间:2017-06-20 07:45:36

标签: php mysqli

好的,我正在做的是从1个表中获取成员id并将这些ID循环到另一个表中以获取要输出的值。我让它在第一个循环中工作,然后注意输出都是搞乱的,所以我需要再次循环它,因为在第二个查询中可能会有多个条目用于MemID。现在我放入第二个循环,但它甚至没有通过,不知道我搞砸了代码的地方,但是现在在通过第二个循环运行时似乎没有输出。虽然它确实没有循环输出。但由于每个$ memid有多行,因此无效。

$qry_skamembers = "SELECT * FROM ap_skamembers ORDER BY id";
$qry_skamembers = $mysqli->real_escape_string($qry_skamembers);
if($result_skamembers = $mysqli->query($qry_skamembers)){
    while($row_skamembers = $result_skamembers->fetch_array()){
        $AffID = $row_skamembers['AffID'];
        $MemID = $row_skamembers['MemberID'];

        $skacon = new mysqli(OW_DB_HOST, OW_DB_USER, OW_DB_PASSWORD, OW_DB_NAME);
        if ($skacon->connect_error) {
            die('Connect Error');
        }

        $get_data = "SELECT * FROM ow_base_billing_sale WHERE userID = $MemID AND status = 'delivered' ORDER BY id";
        $get_data = $skacon->real_escape_string($get_data);
        if($result_data = $skacon->query($get_data)){
            while($finish = $result_data->fetch_array()){
                $test = $finish['status'];
                if($test == 'delivered') {

                    $sale_amount = $finish['price'];
                    $product = $finish['transactionUId'];

                    $products = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT * FROM ap_earnings where product = $product"));
                    if(mysqli_num_rows($products) > 0) { }
                    else {
                        echo "AFF: " . $AffID . "  |  ";
                        echo "Mem: " . $MemID . "  |  ";
                        echo "PRICE: " . $sale_amount . "  |  ";
                        echo "PRODUCT: " . $product . "  --  ";
                        include('controller/record-sale.php');
                        echo "inserting record";
                        echo "run finished <br><br>";    
                    }    
                }
            }  
        }      
    }
}

我仍然很擅长编码,所以看起来有点草率,我为此道歉。

2 个答案:

答案 0 :(得分:0)

我不知道是不是试图替换:

$get_data = $skacon-->real_escape_string($get_data);

$get_data = $skacon->real_escape_string($get_data);

我认为还有一个额外的-

正如我们在评论中发现的那样,您需要将$product更改为'$product'

答案 1 :(得分:0)

你可以在一个查询中完成所有这些操作,不用担心它会更快

SELECT aps.AffID,aps.MemberID,owb.price,owb.transactionUId 
FROM db1.ap_skamembers aps JOIN db2.ow_base_billing_sale owb
  ON aps.MemberID = owb.userID AND owb.status='delivered' 
JOIN  db3.ap_earnings ape 
  ON ape.product = owb.transactionUId  
ORDER BY aps.id