Jay
Jay

Reputation: 1033

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:

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.

Upvotes: 0

Views: 88

Answers (4)

Matteo Tassinari
Matteo Tassinari

Reputation: 18584

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.

If you know that there will be at most one result, you could do this:

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

Upvotes: 0

a coder
a coder

Reputation: 7659

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

$row is an array. You are adding this to the sqlResultsArray.

Upvotes: 0

Marc B
Marc B

Reputation: 360752

You're using the mysql_fetch function_array() function, which returns the row of data as an array. You then insert this array into your results array, so... yeah... multidimensional because you're fetching/building it that way.

Upvotes: 0

Tarun
Tarun

Reputation: 3165

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

Upvotes: 1

Related Questions