返回多行时,PHP mysqli查询结果重复最后一行

时间:2017-04-04 11:21:11

标签: php mysqli

大家好我有以下功能:

Function Query($sql, $parameters, &$results, $result_types = null)
{
    if($this->debug)
        $this->OutputDebug('Query: '.$sql);
    $results = array();
    $statement = $this->db->stmt_init();
    if(!$statement->prepare($sql))
        return $this->SetError($statement->error);
    $prepared = array();
    $types = '';
    $tp = count($parameters);
    $v = $parameters;
    for($p = 0; $p < $tp;)
    {
        switch($t = $v[$p++])
        {
            case 's':
            case 'i':
            case 'd':
                $v[$p] = (IsSet($v[$p]) ? $v[$p] : null);
                break;
            case 'b':
                $v[$p] = (IsSet($v[$p]) ? ($v[$p] ? 'Y' : 'N') : null);
            case 't':
            case 'dt':
            case 'ts':
                $t = 's';
                break;
        }
        $types .= $t;
        if($this->debug)
            $this->OutputDebug('Query parameter type: '.$t.' value: '.$v[$p]);
        $prepared[] = &$v[$p++];
    }
    array_unshift($prepared, $types);
    if(!call_user_func_array(array($statement, 'bind_param'), $prepared)
    || !$statement->execute())
    {
        $error = $statement->error;
        $statement->close();
        return $this->SetError($error);
    }
    $fields = $statement->field_count;
    if($fields)
    {
        $row = $bind = array();
        for($f = 0; $f < $fields; ++$f)
        {
            $row[$f] = null;
            $bind[$f] = &$row[$f];
        }
        if(!call_user_func_array(array($statement, 'bind_result'), $bind))
        {
            $statement->close();
            return $this->SetError($statement->error);
        }
        $rows = array();
        $i = 0;
        while(($success = $statement->fetch()) !== null)
        {
            if(!$success)
            {
                $statement->close();
                return $this->SetError($statement->error);
            }
            if(IsSet($result_types))
            {
                $tc = count($result_types);
                for($c = 0; $c < $tc; ++$c)
                {
                    if(!IsSet($row[$c]))
                        continue;
                    switch($result_types[$c])
                    {
                        case 'b':
                            $row[$c] = ($row[$c] === 'Y');
                            break;
                    }
                }
            }
            $rows[] = $row;
        }
        $results['rows'] = $rows;
    }
    elseif(strlen($error = $statement->error))
    {
        $statement->close();
        return $this->SetError($error);
    }
    else
    {
        $results['insert_id'] = $statement->insert_id;
        $results['affected_rows'] = $statement->affected_rows;
    }
    $statement->close();
    return true;
}

现在让我们假设一个名为roster的简单表,如下所示:

  • id球员队
  • 116 6 2
  • 117 6 3
  • 118 3 5

现在让我们调用函数:

$parameters = array(            
    'i', 0
);
$result_types = array('i');
$this->Query( 'SELECT id FROM roster WHERE player != ? ORDER BY id', $parameters, $results, $result_types);

从此我希望得到116,117&amp; 118作为数组的一部分从$ results返回。相反,我得到118,118&amp; 118 !!

print_r(array_values($ results));返回:

Array ( [0] => Array ( [0] => Array ( [0] => 118 ) [1] => Array ( [0] => 118 ) [2] => Array ( [0] => 118 ) ) )

我在这里做错了什么?

0 个答案:

没有答案