简体   繁体   English

将结果从MYSQL查询推入数组

[英]Push results from MYSQL Query into an Array

I'm having trouble with the function below. 我在使用以下功能时遇到麻烦。 Basically what it is supposed to do is check IF each item from the array ( $idDiscontinuedArray ) it's discontinued value from its respective table (either 1 or 0 ). 基本上,应该执行的操作是检查数组( $idDiscontinuedArray )中的每个项目是否是其各自表中的不连续值( 10 )。

Now I'm not sure to push these answers in an array to make the next part more simpler. 现在,我不确定将这些答案放入数组中以使下一部分更加简单。 At the moment the its spits $rows out individually. 目前,它的吐痰$rows逐出。

The result is: Array ( [discontinued] => 1, ) Array ( [discontinued] => 1 ) Array ( [discontinued] => 0 ) Array ( [discontinued] => 0 ) Array ( [discontinued] => 1 ).... where I would rather have Array [1] => 1 [2] => 1 [3] => 0 [4] => 0 [5] => 1.... 结果为: 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....

The next part of the script is to check and see whether that ALL $rows = 1 which means end of script. 脚本的下一部分是检查所有$rows = 1是否表示脚本结束。 If this is not the case this will run the function changeDiscontinued($dbh, $id, $idDiscontinuedArray) . 如果不是这种情况,它将运行function 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();
    }
}

I Did not test this code, but it goes over prepared statements, and how to push items into an array. 我没有测试此代码,但它遍历了准备好的语句以及如何将项目推入数组。 If you want more help, we can go into chat. 如果您需要更多帮助,我们可以聊天。

$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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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