0

I query the db and when I use print_r() I get the expected output, but when I feed the output into a foreach loop I don't get the values.

My Class with it's method:

class select_data {

    public function get_parent_id($parent_email) {
        //$parent_ids = array();

        try {

            $db = new database;
            $conn = $db->databaseConnect();

            $select_parent_ids_stmt = $conn->prepare('SELECT uid, parent_id, email_of  FROM emails WHERE email_address =:parent_email LIMIT 2');

            $select_parent_ids_stmt->bindParam(':parent_email', $parent_email, PDO::PARAM_STR);

            $select_parent_ids_stmt->execute();

            if($select_parent_ids_stmt->rowCount() === 1){
                $parent_ids = $select_parent_ids_stmt->fetchAll(PDO::FETCH_OBJ);
            }
            $conn = NULL;

        } catch (PDOException $e) {
            echo 'Could not process your request at this time.';
        }

        //Resturn the result
        return $parent_ids;
    }
}

Here I call the the method:

$select_parent_ids = new select_data;
$parent_ids = $select_parent_ids->get_parent_id($parent_email);

From here I use print_r($parent_ids) to see if I get the expected result and I do: Print_r screenshot

Then I have my foreach loop. This is the code:

foreach ($parent_ids as $key => $value) {
       $db_uid = $value->uid;
       $parent_id = $value->parent_id;
       $email_of = $value->email_of;
       return $email_of;
}

After this, I am doing stuff based on the result, but that's not working because I only get a blank return where I would expect to get a return of Parent (based on the screenshot provided. I also tried $email_of = $value['email_of'] with the same empty result.

Can ant please try and look where I went wrong?

Ryan Lee
  • 339
  • 2
  • 8
Willem
  • 123
  • 9
  • 2
    With your `return` statement inside the `foreach`, this will iterate only once. This may or may not be a problem at this time but it does seem odd. – Phil Nov 22 '18 at 01:49
  • 2
    You could try some more debugging like `var_dump($value)` within the `foreach` loop. What does the calling code look like? What are you doing with the return value? – Phil Nov 22 '18 at 01:50
  • @Phil true, it will only iterate once, but I should still see the output of Parent? – Willem Nov 22 '18 at 01:51
  • And as always, make sure you can see any and all errors that might be occurring. See [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Nov 22 '18 at 01:51
  • @Phil I need to insert more data into the DB based on the returned result. – Willem Nov 22 '18 at 01:54
  • I hope you can understand that a comment like that does not answer my questions. Please [update your question](https://stackoverflow.com/posts/53422784/edit) with actual code. – Phil Nov 22 '18 at 01:55
  • @Phil I updated question will all the involved code. – Willem Nov 22 '18 at 02:01
  • Sorry, by _"calling code"_ I meant whatever code receives the value from `return $email_of;`. Where is that code? Also, you have a lot of potential errors regarding undefined variables. Again, see [this post](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) to make sure you can see those errors. – Phil Nov 22 '18 at 02:03
  • @Phil There are no errors showing up. I have error reporting turned on. With regards to the calling code, this is the code that receives the data and works with it. This code sits right under a `elseif` statement. – Willem Nov 22 '18 at 02:14
  • _"this is the code..."_ you have not shown it. Please edit your question – Phil Nov 22 '18 at 02:15
  • Please explain and include code samples, how you are verifying this _problem_. Where is the code that you expect to show the _"Parent"_ value but is not? – Phil Nov 22 '18 at 02:20
  • I guess just `foreach ($parent_ids as $value) {` ? – Alex Nov 22 '18 at 02:29
  • @Phil, there is no other code. Prior to this code some information was already added into the DB. This method of which the code is above is then called. I don't understand why with `print_r`I don't see the result. This tells me that there is nothing wrong with my query to the DB. Because `foreach` refuse to give me the desired values the code after this fails because it receives no data. That code is `if($email_of === 'Parent'){ $insert_rel_conc = new insert_data; $rel_conc = insert_data->insert_relationship_concent($uid, $parent_id, db_uid,$declaration_agreement_parent);` – Willem Nov 22 '18 at 02:29
  • @Alex `($parent_ids as $value)`is also not working. This is how I had it to start off with and then changed it to the current `($parent_ids as $key => $value)` – Willem Nov 22 '18 at 02:35
  • Ah, the penny drops... when you execute `return ...`, **nothing** after that executes. – Phil Nov 22 '18 at 04:37

0 Answers0