簡體   English   中英

更改PHP數組輸出

[英]Changing the php array output

我有這個功能

public function select($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null){

        $q = 'SELECT '.$rows.' FROM '.$table;
    if($join != null){
        $q .= ' JOIN '.$join;
    }
    if($where != null){
            $q .= ' WHERE '.$where;
        }
        if($order != null){
                $q .= ' ORDER BY '.$order;
    }
        if($limit != null){
                $q .= ' LIMIT '.$limit;
        }

    if($this->tableExists($table)){

        $query = @mysql_query($q);
        if($query){

            $this->numResults = mysql_num_rows($query);

            for($i = 0; $i < $this->numResults; $i++){
                $r = mysql_fetch_array($query);
                $key = array_keys($r);
                for($x = 0; $x < count($key); $x++){

                    if(!is_int($key[$x])){
                        if(mysql_num_rows($query) > 1){
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        }else if(mysql_num_rows($query) < 1){
                            $this->result = null;
                        }else{
                            $this->result[$key[$x]] = $r[$key[$x]];
                        }
                    }
                }
            }
            return true;
        }else{
            array_push($this->result,mysql_error());
            return false; // No rows where returned
        }
    }else{
        return false;
    }
}

如果我使用此功能並且我的數據庫為空,則我的數組看起來像這樣

Array( )

這可以!

如果我的數據庫有1個條目,則如下所示:

Array
(
    [id] => 1
    [a] => 0
    [b] => 0
)

這不是很好,因為如果條目數多於1,則看起來像這樣:

Array
(
    [0] => Array
        (
            [id] => 1
            [a] => 0
            [b] => 0
        )

    [1] => Array
        (
            [id] => 2
            [a] => 1
            [b] => 1
        )

)

我想要的是具有一個條目的輸出看起來像這樣:

Array
(
    [0] => Array
        (
            [id] => 1
            [a] => 0
            [b] => 0
        )
)

我如何更改功能以獲得此輸出?

更改

if(mysql_num_rows($query) > 1){
    $this->result[$i][$key[$x]] = $r[$key[$x]];
}else if(mysql_num_rows($query) < 1){
    $this->result = null;
}else{
    $this->result[$key[$x]] = $r[$key[$x]];
}

if(mysql_num_rows($query) >= 1){
    $this->result[$i][$key[$x]] = $r[$key[$x]];
}else{
    $this->result = null;
}

您的錯誤所在的行:

if(mysql_num_rows($query) > 1){

您必須考慮到第一行,因此“> 1”變為“> 0”

整個功能應如下所示:

public function select($table, $rows = '*', $join = null, $where = null, $order = null, $limit = null){

    $q = 'SELECT '.$rows.' FROM '.$table;
if($join != null){
    $q .= ' JOIN '.$join;
}
if($where != null){
        $q .= ' WHERE '.$where;
    }
    if($order != null){
            $q .= ' ORDER BY '.$order;
}
    if($limit != null){
            $q .= ' LIMIT '.$limit;
    }

if($this->tableExists($table)){

    $query = @mysql_query($q);
    if($query){

        $this->numResults = mysql_num_rows($query);

        for($i = 0; $i < $this->numResults; $i++){
            $r = mysql_fetch_array($query);
            $key = array_keys($r);
            for($x = 0; $x < count($key); $x++){

                if(!is_int($key[$x])){
                    if(mysql_num_rows($query) > 0)
                        $this->result[$i][$key[$x]] = $r[$key[$x]];
                    else 
                        $this->result = null;                        
                }
            }
        }
        return true;
    }else{
        array_push($this->result,mysql_error());
        return false; // No rows where returned
    }
}else{
    return false;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM