php - 当我在数组中获取时,每列仅显示一个元素

标签 php mysql arrays

当我从数组中的数据库中获取数据时,它只显示每一列的一个元素。我正在使用以下查询:

 $stmt = $conn_obj->select(' user ',' * ',' status = "updated" ', $order=NULL, $group=null, $fromRecordNum.','.$recordsPerPage);

选择功能如下:

private function tableExists($table) {
    //$this->con = $this->createconnection();
    $query = 'SHOW TABLES FROM ' . $this->db . ' LIKE "'.trim($table).'"';
    //echo ''.$query;
        $tablesInDb = mysqli_query($this->con, $query);
        if ($tablesInDb) {
            if (mysqli_num_rows($tablesInDb) == 1) {
                //echo ''.mysqli_num_rows($tablesInDb);
                return true;
            } 
            else {
                return false;
            }
        }
}
 public function select($table, $row = '*', $where= null,$order=null,$group=null, $limit=null, $join=null){
    //$this->con = $this->createconnection();
    //echo $join;
    $q = 'select'.$row.' from '.$table;
    //print_r($q);
    if($join != null){
        $q .= ' join '.$join;

    }
    if($where != null){
        $q .= ' where '.$where;
        print_r($q);
    }
     if($group != null){
        $q .= 'group by'.$group;
        //print_r($q);
    }
     if($order != null){
        $q .= 'order by'.$order;

    }       
     if($limit != null){
        $q .= 'limit '.$limit;
       print_r($q);
    }

   if ($this->tableExists($table)) { 
        $query = mysqli_query($this->con, $q)  or die(mysql_error());
        //print_r($query);
        if ($query) {
            $this->numResults = mysqli_num_rows($query);
            //echo $this->numResults;
            for ($i = 0; $i < $this->numResults; $i++) {
                $r = mysqli_fetch_array($query);
                $key = array_keys($r);

                for ($x = 0; $x < count($key); $x++) {
                    // Sanitizes keys so only alphavalues are allowed  
                    if (!is_int($key[$x])) {
                        if (mysqli_num_rows($query) > 1)
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        else if (mysqli_num_rows($query) < 1)
                            $this->result = null;
                        else
                            $this->result[$key[$x]] = $r[$key[$x]];
                    }
                }
            }
            //print_r($this->result);
            return $this->result;
        } else {

            return false;
        }
    } else{

        return false;
    }
}

我使用 foreach 循环获取它,如下所示:

foreach($stmt as $row)
{  
    echo $row['user_Id'];
}

输出= 7 t : 2 2 2 u W u 2
如果我用 print_r($row) 打印整个数组,那么
输出是= test::1 2015-07-30 11:42:09am 2015-07-29 12:42:09pm 2015-07-30 12:28:57pm 更新call_activated uninstall 2015-07-30 12:31:07pm .

如果数据库只有一行具有指定的查询,则出现上述问题。
但在具有指定查询的两行的情况下,它可以正常工作,如我所愿。

最佳答案

对于多个结果,您将获得一个元素数组的数组。
对于单个结果,您将获得一个元素数组。 因此,对于单个元素,一个 foreach 循环太多了。 如果在你的例子中:

foreach($stmt as $row) {  
    echo $row['user_Id'];
}

假设 $stmt 包含查询的返回值:

foreach($stmt as $row) {
    if (isset($row['user_Id'])) {
        // do something with one single result record
    } else {
        foreach ($row AS $innerRow) {
            // do something with each result record
        }
    }
}

关于php - 当我在数组中获取时,每列仅显示一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31744608/

相关文章:

php - 使用 Twig 解析 txt 文件

mysql - 即使 mysql 中的值为零,如何在 Q1、Q2、Q3、Q4 中显示值为零

MySQL BETWEEN 运算符不适用于 DATETIME 列

javascript - 根据属性从对象数组创建对象

arrays - 在Perl中,如何生成列表的所有可能组合?

php - 我为 strip &lt;script&gt;&lt;/script&gt; 标签创建了这个表单,但是这个表单不起作用

php - 网站设计阻止数据发布到服务器

php - MySQL 更新不工作

php - 从数据库 SQL 中的唯一 ID 获取最后一个数字 - PHP CodeIgniter

c - 函数未正确将数据读入数组