0

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.

Eva Dias
  • 1,709
  • 9
  • 36
  • 67
Jay
  • 1,033
  • 1
  • 18
  • 31
  • `->sQLResults()` is oddly named, but sounds like it returns multiple possible results at all time (= a list of result rows). Note that you are not using the `->selectAndQuery` wrapper in your first code example. – mario Oct 03 '12 at 16:34

4 Answers4

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);}
Tarun
  • 3,162
  • 3
  • 29
  • 45
  • 1
    The OP's function isn't limited to single-row-result queries, this'd trash all but the LAST row of any query which SHOULD return multiple rows. – Marc B Oct 03 '12 at 16:34
  • Thanks Tarun, my results aren't limited but this technique can be used and the conditions changed. Thanks alot :) – Jay Oct 03 '12 at 16:49
  • As @MarcB state, you must be aware that, if you try to retrieve multiple rows and it only return one row(with the where clause), the behaviour will not be the same. – codea Oct 03 '12 at 16:58
0

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0
    while ($row = mysql_fetch_array($a)) {
    $this->sqlResultsArray[] = $row; 

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

a coder
  • 7,530
  • 20
  • 84
  • 131
0

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];
Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81