3

Why is my method only returning the first row of my table? I can't understand why and it's driving me nuts. I'm sure it's something very simple.

public function getTitlesForRegistrationForm() {
    $result = $this->_db->query("SELECT UserTitleID, UserTitleName FROM UserTitles");
    $i=0;
    $array[0] = "No result";
    foreach($result->fetch(PDO::FETCH_ASSOC) as $row){
        $array[$i] = $row;
        $i++;
    }
    return $array;
}

Thanks.

3
  • 6
    $result->fetch() returns a single row with each call. You are only calling it once … Commented Feb 20, 2012 at 15:00
  • 1
    Are you using PDO library? I think you should use fetchAll() instead of fetch() Commented Feb 20, 2012 at 15:01
  • 1
    Have you run the query with a tool like phpMyAdmin to see what the result is? It's possible your query has a single result. Commented Feb 20, 2012 at 15:02

6 Answers 6

7

It may be because your $result->fetch() call doesn't return an iterable value, but either a result row or FALSE. PHP's foreach only works on iterable values. Updating your code to something like this should do the trick:

public function getTitlesForRegistrationForm() {
    $result = $this->_db->query("SELECT UserTitleID, UserTitleName FROM UserTitles");
    $i=0;
    $array[0] = "No result";
    while (($row = $result->fetch(PDO::FETCH_ASSOC)) !== FALSE) {
        $array[$i] = $row;
        $i++;
    }
    return $array;
}
Sign up to request clarification or add additional context in comments.

Comments

3

You need to fetch() inside a while loop. It will return only one row each time it is called.

I've also taken the liberty of refactoring away your $i counter. Instead the No result is appended onto the array in the first position (for whatever purpose you planned to use it), and subsequent rows are appended on with [].

public function getTitlesForRegistrationForm() {
    $array = array();
    $result = $this->_db->query("SELECT UserTitleID, UserTitleName FROM UserTitles");

    // Why are you putting No Result onto the array?
    // I've left it in, but it doesn't make sense to me.
    $array[] = "No result";
    while ($row = $result->fetch(PDO::FETCH_ASSOC)){
        $array[] = $row;
    }
    return $array;
}

3 Comments

@DjangoReinhardt I understood why you're accumulating the result set onto an array, I don't understand why you're adding "No result" at the beginning.
Ah, I see. Error handing, so I can see that something isn't working. Probably not the best way. There's no need for it, so your answer is actually best (and first). Thanks!
3

It's because you are using fetch(). You need fetchall().

http://www.php.net/manual/en/pdostatement.fetchall.php

[edit]Wow. I need to learn to type faster.

1 Comment

You're right, that did work... sort of. I got some crap in the Array.
2

Try:

while($row = $result->fetch(PDO::FETCH_ASSOC)){
    foo();
}

Comments

2

Try while instead of foreach. Foreach only iterates over a single row that is returned. code:

while($row = fetch(PDO::FETCH_ASSOC)) {
  $array[$i] = $row;
  $i++;
}

Comments

2

Just replace the fetch method with fetchAll

1 Comment

You're right, that did work... sort of. I got some crap in the Array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.