将MYSQL查询的结果推送到数组中

时间:2014-01-15 02:33:57

标签: php arrays function if-statement foreach

我在使用下面的功能时遇到问题。基本上它应该做的是检查IF中的每个项目($idDiscontinuedArray)它是否从其各自的表(10)中断了值。

现在我不确定在阵列中推送这些答案,以使下一部分更简单。目前,它单独吐出$rows

结果是:Array ( [discontinued] => 1, ) Array ( [discontinued] => 1 ) Array ( [discontinued] => 0 ) Array ( [discontinued] => 0 ) Array ( [discontinued] => 1 )....我希望Array [1] => 1 [2] => 1 [3] => 0 [4] => 0 [5] => 1....

脚本的下一部分是检查并查看是否所有$rows = 1表示脚本结束。如果不是这种情况,则会运行函数changeDiscontinued($dbh, $id, $idDiscontinuedArray)

function checkDiscontinued($dbh, $idDiscontinuedArray) {
try {
    foreach ($idDiscontinuedArray as $id) {
        $stmt = $dbh->query("SELECT discontinued FROM `$id` ORDER BY `date` DESC LIMIT 1");
        $rows = $stmt->fetch(PDO::FETCH_ASSOC);
        print_r($rows);
        }
        if $rows['discontinued'] == TRUE) { 
            //echo $id . "Action if true";
        } else {
            changeDiscontinued($dbh, $id, $idDiscontinuedArray);
            echo $id . "Items already discontinued!";
            }       
    }
    catch (PDOException $e) {
    echo $e->getMessage();
    }
}

1 个答案:

答案 0 :(得分:1)

我没有测试过这段代码,但是它已经过了准备好的语句,以及如何将项目推送到数组中。如果您需要更多帮助,我们可以进行聊天。

$discont = array();

function checkDiscontinued($dbh, $idDiscontinuedArray) {
try {
    foreach ($idDiscontinuedArray as $id) {
        $sql = $dbh->prepare("SELECT discontinued FROM $id ORDER BY `date` DESC LIMIT 1");
        $stmt = $sql->execute(array($id));
        $rows = $stmt->fetch(PDO::FETCH_ASSOC);
        print_r($rows);
        }
        if $rows['discontinued'] == TRUE) { 
            //echo $id . "Action if true";
        } else {
            changeDiscontinued($dbh, $id, $idDiscontinuedArray);
            echo $id . "Items already discontinued!";
            array_push($doscont, $id);
            }       
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    }
}