0

大家好。我目前正在开发一个处理用户登录的登录类。通过使用以下代码将其创建为对象,我从数据库中获取了我的数组列表:

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

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
    )
)

现在,从它的外观来看,它是一个包含在数组中的数组,这有点令人困惑,因为创建这个数组的代码并没有引发这种情况(我可能是错的,不幸的是,这是我需要指针的地方)。设置数组的代码是:

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

所以我对你们的问题是,是什么导致数组变维,我能做些什么来阻止它?我知道我可以从多维数组中提取信息,但我试图让事情尽可能简单。

任何帮助将非常感激。非常感谢。

4

4 回答 4

1
$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);}
于 2012-10-03T16:33:28.633 回答
0

您正在使用 mysql_fetch function_array() 函数,它将数据行作为数组返回。然后,您将此数组插入到结果数组中,所以...是的...多维,因为您正在以这种方式获取/构建它。

于 2012-10-03T16:33:44.110 回答
0
    while ($row = mysql_fetch_array($a)) {
    $this->sqlResultsArray[] = $row; 

$row 是一个数组。您正在将此添加到 sqlResultsArray。

于 2012-10-03T16:33:51.287 回答
0

您的选择只是返回与查询参数匹配的所有行的数组,因此在索引 0 处您将拥有第一行,在索引 1 处拥有第二行,依此类推。

如果你知道最多会有一个结果,你可以这样做:

$sqllogin = $dBquery->sQLResults();
$sqllogin = $sqllogin[0];
于 2012-10-03T16:35:11.097 回答