简体   繁体   English

我的关联数组是多维的,我不知道为什么。 的PHP

[英]My Associative array is multi dimensional, and i can't figure out why. PHP

Hi all. 大家好。 I'm currently in the middle of developing a login class that handles the user login. 我目前正在开发处理用户登录的登录类。 I get my array list back from the DB by creating it as an object with the following code: 通过使用以下代码将其创建为对象,可以从数据库获取数组列表:

 $dBquery = new Selection();
 $dBquery->setUpSelection($sqlConstructor);
 $sqllogin = $dBquery->sQLResults();
 print_r($sqllogin);

and from the print_r , this is what's returning: print_r ,返回的是:

Array (
    [0] => Array (
        [0] => 1
        [uSID] => 1
        [1] => 0
        [level] => 0
        [2] => a
        [Username] => > a
        [3] => a
        [password] => a
        [4] => a
        [email] => a
        [5] => 0
        [lockedID] => 0
    )
)

Now, from the looks of it, it's an array with in an array, which is a bit confusing since the code to create this array doesn't instigate that (I could be wrong, this is unfortunately where I need pointers). 现在,从它的外观看,它是一个数组,该数组中有一个数组,这有点令人困惑,因为创建该数组的代码并没有暗示这一点(我可能是错的,不幸的是,这是我需要指针的地方)。 The code that sets up the array is: 设置数组的代码是:

private function selectAndQuery() {
        //Select query with AND
        $sql2 ="SELECT * FROM ".$this->table." WHERE ".$this->column."='".$this->where."' AND ".$this->andColumn."='".$this->sqlAnd."'  ";
        $a = mysql_query($sql2) or die(mysql_error());      
        //get number of rows to determine if there are any results
        if (mysql_num_rows($a) < 1) {
                    //TO DO no results returned.
            $this->sQLResults();
        } else {
            //if there's a result store it into an array.
            while ($row = mysql_fetch_array($a)) {
            $this->sqlResultsArray[] = $row; 
            }           
            $this->sQLResults();
        }
    }

So my question to you guys is, what's causing the array to go dimensional and what can I do to stop it? 所以我对你们的问题是,是什么引起数组维数变化,我该怎么做才能停止它? I know I could just pull the information from the multi-dimensional array but I was trying to keep things as simple as possible. 我知道我可以从多维数组中提取信息,但是我试图使事情尽可能简单。

Any help would be much appreciated. 任何帮助将非常感激。 Many thanks. 非常感谢。

$this->sqlResultsArray[] = $row; //This is causing you to get multidimensional array
since  mysql_fetch_array returns an array.So if you are sure that you will get only one
row.you can use below given mehod instead
if(mysql_num_rows() = 1){$this->sqlResultsArray  = mysql_fetch_array($a);}

You're using the mysql_fetch function_array() function, which returns the row of data as an array. 您正在使用mysql_fetch function_array()函数,该函数将数据行作为数组返回。 You then insert this array into your results array, so... yeah... multidimensional because you're fetching/building it that way. 然后,您将此数组插入到结果数组中,所以...是的...多维的,因为您正在以这种方式获取/构建它。

    while ($row = mysql_fetch_array($a)) {
    $this->sqlResultsArray[] = $row; 

$row is an array. $ row是一个数组。 You are adding this to the sqlResultsArray. 您将其添加到sqlResultsArray。

Your selection is simply returning an array of all rows matching the query parameters, so at index 0 you'd have the first row, at index 1 the second row, and so on. 您的选择只是返回一个与查询参数匹配的所有行的数组,因此在索引0处您将拥有第一行,在索引1处将拥有第二行,依此类推。

If you know that there will be at most one result, you could do this: 如果您知道最多会有一个结果,则可以执行以下操作:

$sqllogin = $dBquery->sQLResults();
$sqllogin = $sqllogin[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM