1

I have a function, which checks a query, and then uses a while loop to extract data, I would like to return this data but I still want it to increment, but I read that return terminates a function, and thus would make it unable to increment. Is there a way around this?

$this->messages = array();
while($row = $data) {
                $this->messages[$i]['id'] = $row['id'];
                $this->messages[$i]['title'] = $row['title'];
                $this->messages[$i]['message'] = $row['message'];
                $i++;

This loop is inside the function, I want to continue until the loop is done, but then I can't return any values.. is there a work around this?

Thank you

EDIT:

<?php

function Message($username){

            $query = "SELECT * FROM msg WHERE `to` = '".$this->userid."' && `to_viewed` = '0' && `to_deleted` = '0' ORDER BY `created` DESC";   
            $stmt = $this->connection->prepare($query);
            $stmt->execute();

    $data = $stmt->fetch(PDO::FETCH_ASSOC);
    $num_rows = $stmt->fetchColumn();

        if($num_rows) {
            $i=0;
            $this->messages = array();
            while($row = $data) {
// want to return these 3 for each result
                $this->messages[$i]['id'] = $row['id'];
                $this->messages[$i]['title'] = $row['title'];
                $this->messages[$i]['message'] = $row['message'];

                $i++;
            }
        } else {
            return 1; 
        }
    }

?>
13
  • 1
    There are plenty of ways to return data after completing a loop but you're not being terribly clear about your goals here: can you clarify what you mean by 'I still want it to increment', or give some more of the code surrounding your function? And which values you want to return? Commented Aug 18, 2013 at 1:40
  • @davidf2281 Yes sorry Commented Aug 18, 2013 at 1:42
  • @user2693086 Could possibly a temp variable work? Save the data from the loop in a new variable and then increment that later. Commented Aug 18, 2013 at 1:44
  • @davidf2281 I added it into my original post. Basically, I want the loop to continue but I want to be able to return each result from the query. Commented Aug 18, 2013 at 1:46
  • 1
    You'll need to declare a new temporary results array before the loop; populate the array during the loop with each result; and then return the results array when the loop has finished. Commented Aug 18, 2013 at 1:51

1 Answer 1

4

Use PDO::fetchAll(), it returns an array of all the results, rather than returning one row at a time:

function Message($username){

    $query = "SELECT * FROM msg WHERE `to` = '".$this->userid."' && `to_viewed` = '0' && `to_deleted` = '0' ORDER BY `created` DESC";   
    $stmt = $this->connection->prepare($query);
    $stmt->execute();
    $this->messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $this->messages;
}

But to teach you about loops (a CS 101 concept -- what ever happened to people learning how to program?), here's how you would code it yourself:

    $stmt->execute();
    $this->messages = array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
        $this->messages[] = $row;
    }
    return $this->messages;
Sign up to request clarification or add additional context in comments.

1 Comment

suggested edit: while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ not while ($row = $stmt->fetch(PDO::FETCH_ASSOC);

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.