PHP does not return fetchAll(PDO::FETCH_OBJ) value in foreach





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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?










share|improve this question




















  • 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 1: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 1:50











  • @Phil true, it will only iterate once, but I should still see the output of Parent?

    – Willem
    Nov 22 '18 at 1: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?

    – Phil
    Nov 22 '18 at 1:51











  • @Phil I need to insert more data into the DB based on the returned result.

    – Willem
    Nov 22 '18 at 1:54


















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?










share|improve this question




















  • 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 1: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 1:50











  • @Phil true, it will only iterate once, but I should still see the output of Parent?

    – Willem
    Nov 22 '18 at 1: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?

    – Phil
    Nov 22 '18 at 1:51











  • @Phil I need to insert more data into the DB based on the returned result.

    – Willem
    Nov 22 '18 at 1:54














0












0








0


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?










share|improve this question
















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?







php mysql foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 2:16









Ryan Lee

745




745










asked Nov 22 '18 at 1:42









WillemWillem

618




618








  • 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 1: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 1:50











  • @Phil true, it will only iterate once, but I should still see the output of Parent?

    – Willem
    Nov 22 '18 at 1: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?

    – Phil
    Nov 22 '18 at 1:51











  • @Phil I need to insert more data into the DB based on the returned result.

    – Willem
    Nov 22 '18 at 1:54














  • 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 1: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 1:50











  • @Phil true, it will only iterate once, but I should still see the output of Parent?

    – Willem
    Nov 22 '18 at 1: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?

    – Phil
    Nov 22 '18 at 1:51











  • @Phil I need to insert more data into the DB based on the returned result.

    – Willem
    Nov 22 '18 at 1:54








2




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 1:49





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 1:49




2




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 1:50





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 1:50













@Phil true, it will only iterate once, but I should still see the output of Parent?

– Willem
Nov 22 '18 at 1:51





@Phil true, it will only iterate once, but I should still see the output of Parent?

– Willem
Nov 22 '18 at 1: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?

– Phil
Nov 22 '18 at 1: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?

– Phil
Nov 22 '18 at 1:51













@Phil I need to insert more data into the DB based on the returned result.

– Willem
Nov 22 '18 at 1:54





@Phil I need to insert more data into the DB based on the returned result.

– Willem
Nov 22 '18 at 1:54












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422784%2fphp-does-not-return-fetchallpdofetch-obj-value-in-foreach%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422784%2fphp-does-not-return-fetchallpdofetch-obj-value-in-foreach%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)