-1

I want to be able to return the 3 variables I have listed below, but when I try, I came into a dead end because I had no idea on how to return multiple entries from one function. A function can only return once, and when it does, it terminates, so what's the work around?

function M($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; 
        }
    }

If I want to return the 3 variables, then the iteration would stop, and wouldn't continue until the loop is done.. can anyone help me out? I have been bashing my head over this for a bit.

  • 3
    Seems like an XY problem. Can't you just return an array? – elclanrs Aug 18 '13 at 06:47
  • No.. if I put it into an array: return array(var1, var2, var3), the function will end.. and the iteration stops as well, which defeats the whole point of storing all of the entries found using the query into an array. – user2693390 Aug 18 '13 at 06:51
  • use `while($row = $stmt->fetch(PDO::FETCH_ASSOC)){` –  Aug 18 '13 at 06:51
  • @Akam what difference would that make? $data is already that – user2693390 Aug 18 '13 at 06:52
  • What you want to do is not possible, you can't return 3 variables unless they're inside some data structure, there's no workaround. You'll have to think of using an array or an object... – elclanrs Aug 18 '13 at 06:54
  • 3
    You don't have to return the array immediately. You return it at the end of the function. Just fill it in with everything you need. If you write your conditions/loops properly, it's not a problem at all. – Jayawi Perera Aug 18 '13 at 06:55
  • @Jay Can I just add return this->message after the curly bracket of the while loop? Wouldn't that hold all the information after the loop has been done? – user2693390 Aug 18 '13 at 06:57
  • You could do that. But like you said, that only returns the $this->messages variable. What are the variables that you are trying to return? – Jayawi Perera Aug 18 '13 at 07:08
  • @jay I meant if I had put the return array inside the loop, it would only loop once and return and that's it, so I should put it outside the loop and it will hold those 3 variables with the loaded information from the query? – user2693390 Aug 18 '13 at 07:11
  • If you return $this->message outside the loop, it will return an array of arrays. Each of the 2nd level arrays will have the values for id, title and message. – Jayawi Perera Aug 18 '13 at 07:16
  • Possible duplicate of [Multiple returns from function](http://stackoverflow.com/questions/3451906/multiple-returns-from-function) – miken32 May 29 '16 at 04:01

3 Answers3

1

These two while loop are not equal:

while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
print_r($row);
}

and

$data = $stmt->fetch(PDO::FETCH_ASSOC);
while($row = $data) {

The differences:

  1. The second one will produce an infinite loop if at least one row returned, because $row=$data is always true and will show only first result.
  2. $stmt->fetch(PDO::FETCH_ASSOC) should be called for retrieving rows, so that, each time you call it you retrieve one row (next row). This can't be referenced by simply = assignment function for retrieving all rows.
  3. Result: your code will terminate execution according to maximum execution time.

Another note, according to http://php.net/manual/en/pdostatement.rowcount.php:

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

But you directly used fetchColumn() without using select count(*)

0

You can do either:

OR

  • Return an array with the 3 variables assigned to it
Jayawi Perera
  • 891
  • 1
  • 8
  • 17
0

Why not return the array of data from the query as it is after checking whether there some rows affected and dealing with the 3 variables outside the function because there is no easy way to return 3 variables. Or return a string of the 3 variables using the implode() function.

function M($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();
    $return_data = array();
        if($num_rows) {
          $return_data = $data;
        } else {
          $return_data[] = "Error!";
        }

return $return_data;
    }
Herbert Musoke
  • 317
  • 2
  • 5