PHP:迭代另一个函数和输出结果的函数

时间:2011-07-31 23:42:58

标签: php mysql loops for-loop

看起来这很简单,但是,我遇到了一个问题:

以下是代码:

function getValidCustomers() {
    global $db;
    $getCustomers = $db->GetAll("SELECT * from customers where CustomerActive='1' AND protected IS NULL or protected=0;");
    foreach($getCustomers as $customer) {
        echo $customer['CustomerID']."\n";
    }
}

function updateValidCustomers() {
    $customers = getValidCustomers();
    for ($i = 0; $i < sizeof($customers); $i++) {
        echo "DEBUG: $customers[$i]\n";
    }
}

updateValidCustomers();

基本上,现在的输出是CustomerID的列表(来自updateValidCustomers())。我只是希望updateValidCustomers()getValidCustomers()获取数据,然后循环遍历它,这样我就可以在其上运行另一个实际操作数据库的查询。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

getValidCustomers不会返回任何内容,也许你的意思是:

function getValidCustomers() {
    global $db;
    $getCustomers = $db->GetAll("SELECT * from customers where CustomerActive='1' AND protected IS NULL or protected=0;");
    foreach($getCustomers as $customer) {
        echo $customer['CustomerID']."\n";
    }
    return $getCustomers;
}

答案 1 :(得分:2)

getValidCustomers()不返回任何内容 - 它只是回显

return $getCustomers添加到getValidCustomers()

的末尾

答案 2 :(得分:1)

return $getCustomers;添加到getValidCustomers():D