dtwknzk3764 2013-11-06 17:17
浏览 24
已采纳

更改php数组输出

i have this function

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;
    }
}

If i use this function and my database is empty, my Array looks like this

Array( )

this is fine!

If my database has 1 entries, it looks like this:

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

This is not fine, because, if it has more entries than 1 it looks like this:

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

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

)

What I want, is that the output with one single entry looks like this:

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

How do I have to change the function to get this output?

  • 写回答

2条回答 默认 最新

  • dongpo1846 2013-11-06 17:23
    关注

    change

    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]];
    }
    

    with

    if(mysql_num_rows($query) >= 1){
        $this->result[$i][$key[$x]] = $r[$key[$x]];
    }else{
        $this->result = null;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答