如何从php mysqli中的结果集中删除行

时间:2014-07-09 15:57:37

标签: php mysql mysqli resultset

基于某些条件,我希望能够从php中的$mysqli对象返回的结果集中删除一行。有谁知道怎么做?

<?php
... 
    $result_set = $mysqli->query('select * from schema.table1;');

    while ($row = $result_set->fetch_assoc()){
        if (/* some condition */){
            //remove this row from the result set
        }
    }

    $result_set->data_seek(0);
    //now the result set has less rows than it did to begin with
    ....
?>

1 个答案:

答案 0 :(得分:2)

您无法对结果集执行此操作。你最好的选择是建立一个阵列。

<?php
... 
    $valid_results = array();
    $result_set = $mysqli->query('select * from schema.table1;');

    while ($row = $result_set->fetch_assoc()){
        if (/* some condition */){
            continue;
        }
        $valid_results[] = $row;
    }

    //do stuff with $valid_results
    ....
?>