PHP PDO: Call to a member function fetch() on boolean










0















So I'm currently trying to do a PDO SELECT Request, but when executing and fetching the extracted data, this error shows up:



1 - Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53 
2 - Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53


This is where I call the function:



include 'includes/user.inc.php';
$userOBJ = new User;
if($userOBJ->isAdmin($_SESSION['session_u-name']))
AdminControl();



Code:



public function isAdmin($user)
$userToGet = $user;

$stmt = $this->Connect()->prepare("SELECT * FROM user_secure WHERE username_db=?");
$query1 = $stmt->execute([$userToGet]);

if(!$query1)

die("Execute query error, because: ". print_r($this->Connect()->errorInfo(),true) );
else
foreach ($query1->fetch() as $row)
if($row['admin_db'] == 1)
return true;
else
return false;






The first error says that I'm not handling the PDO errors, which I think that I'm already handling any PDO error in my code, but somehow still gets detected as I'm not doing so... (Correct me if wrong)



Second error states that calling PDO->fetch() is returning a boolean, but I'm requesting data, so it's not able to continue with the following code...
I don't get why this is showing... The "username_db" var in the query is the same as the one that I have in my DB.



In the same file as the function above, I have this next function and when called, it does fine



public function RegisterUser($user, $pwd, $mail)
$u_Insert = $user;
$p_Insert = $pwd;
$m_Insert = $mail;

$stmt = $this->Connect()->prepare("INSERT INTO user_secure(username_db, password_db) VALUES (?,?)");
$query1 = $stmt->execute([$u_Insert, $p_Insert]);

$stmt = $this->Connect()->prepare("INSERT INTO user_info(mail_db) VALUES (?)");
$query2 = $stmt->execute([$m_Insert]);

if($query2 && $query1)
return true;
else
return false;




Is there something that I'm missing?
I have already checked this thread but I'm still in the exact position...



Thank you for your time



(I'm still learning PDO, sorry if my code isn't clean)










share|improve this question
























  • What? Which one?

    – Alan Maldonado
    Nov 16 '18 at 2:58











  • the question has 5 tags; one being jquery.

    – Funk Forty Niner
    Nov 16 '18 at 2:59











  • My bad! Tried to put the tag "query"

    – Alan Maldonado
    Nov 16 '18 at 3:02











  • Ah I see, I understand now.

    – Funk Forty Niner
    Nov 16 '18 at 3:28















0















So I'm currently trying to do a PDO SELECT Request, but when executing and fetching the extracted data, this error shows up:



1 - Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53 
2 - Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53


This is where I call the function:



include 'includes/user.inc.php';
$userOBJ = new User;
if($userOBJ->isAdmin($_SESSION['session_u-name']))
AdminControl();



Code:



public function isAdmin($user)
$userToGet = $user;

$stmt = $this->Connect()->prepare("SELECT * FROM user_secure WHERE username_db=?");
$query1 = $stmt->execute([$userToGet]);

if(!$query1)

die("Execute query error, because: ". print_r($this->Connect()->errorInfo(),true) );
else
foreach ($query1->fetch() as $row)
if($row['admin_db'] == 1)
return true;
else
return false;






The first error says that I'm not handling the PDO errors, which I think that I'm already handling any PDO error in my code, but somehow still gets detected as I'm not doing so... (Correct me if wrong)



Second error states that calling PDO->fetch() is returning a boolean, but I'm requesting data, so it's not able to continue with the following code...
I don't get why this is showing... The "username_db" var in the query is the same as the one that I have in my DB.



In the same file as the function above, I have this next function and when called, it does fine



public function RegisterUser($user, $pwd, $mail)
$u_Insert = $user;
$p_Insert = $pwd;
$m_Insert = $mail;

$stmt = $this->Connect()->prepare("INSERT INTO user_secure(username_db, password_db) VALUES (?,?)");
$query1 = $stmt->execute([$u_Insert, $p_Insert]);

$stmt = $this->Connect()->prepare("INSERT INTO user_info(mail_db) VALUES (?)");
$query2 = $stmt->execute([$m_Insert]);

if($query2 && $query1)
return true;
else
return false;




Is there something that I'm missing?
I have already checked this thread but I'm still in the exact position...



Thank you for your time



(I'm still learning PDO, sorry if my code isn't clean)










share|improve this question
























  • What? Which one?

    – Alan Maldonado
    Nov 16 '18 at 2:58











  • the question has 5 tags; one being jquery.

    – Funk Forty Niner
    Nov 16 '18 at 2:59











  • My bad! Tried to put the tag "query"

    – Alan Maldonado
    Nov 16 '18 at 3:02











  • Ah I see, I understand now.

    – Funk Forty Niner
    Nov 16 '18 at 3:28













0












0








0








So I'm currently trying to do a PDO SELECT Request, but when executing and fetching the extracted data, this error shows up:



1 - Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53 
2 - Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53


This is where I call the function:



include 'includes/user.inc.php';
$userOBJ = new User;
if($userOBJ->isAdmin($_SESSION['session_u-name']))
AdminControl();



Code:



public function isAdmin($user)
$userToGet = $user;

$stmt = $this->Connect()->prepare("SELECT * FROM user_secure WHERE username_db=?");
$query1 = $stmt->execute([$userToGet]);

if(!$query1)

die("Execute query error, because: ". print_r($this->Connect()->errorInfo(),true) );
else
foreach ($query1->fetch() as $row)
if($row['admin_db'] == 1)
return true;
else
return false;






The first error says that I'm not handling the PDO errors, which I think that I'm already handling any PDO error in my code, but somehow still gets detected as I'm not doing so... (Correct me if wrong)



Second error states that calling PDO->fetch() is returning a boolean, but I'm requesting data, so it's not able to continue with the following code...
I don't get why this is showing... The "username_db" var in the query is the same as the one that I have in my DB.



In the same file as the function above, I have this next function and when called, it does fine



public function RegisterUser($user, $pwd, $mail)
$u_Insert = $user;
$p_Insert = $pwd;
$m_Insert = $mail;

$stmt = $this->Connect()->prepare("INSERT INTO user_secure(username_db, password_db) VALUES (?,?)");
$query1 = $stmt->execute([$u_Insert, $p_Insert]);

$stmt = $this->Connect()->prepare("INSERT INTO user_info(mail_db) VALUES (?)");
$query2 = $stmt->execute([$m_Insert]);

if($query2 && $query1)
return true;
else
return false;




Is there something that I'm missing?
I have already checked this thread but I'm still in the exact position...



Thank you for your time



(I'm still learning PDO, sorry if my code isn't clean)










share|improve this question
















So I'm currently trying to do a PDO SELECT Request, but when executing and fetching the extracted data, this error shows up:



1 - Fatal error: Uncaught Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53 
2 - Error: Call to a member function fetch() on boolean in C:wamp64wwwNewKaliincludesuser.inc.php on line 53


This is where I call the function:



include 'includes/user.inc.php';
$userOBJ = new User;
if($userOBJ->isAdmin($_SESSION['session_u-name']))
AdminControl();



Code:



public function isAdmin($user)
$userToGet = $user;

$stmt = $this->Connect()->prepare("SELECT * FROM user_secure WHERE username_db=?");
$query1 = $stmt->execute([$userToGet]);

if(!$query1)

die("Execute query error, because: ". print_r($this->Connect()->errorInfo(),true) );
else
foreach ($query1->fetch() as $row)
if($row['admin_db'] == 1)
return true;
else
return false;






The first error says that I'm not handling the PDO errors, which I think that I'm already handling any PDO error in my code, but somehow still gets detected as I'm not doing so... (Correct me if wrong)



Second error states that calling PDO->fetch() is returning a boolean, but I'm requesting data, so it's not able to continue with the following code...
I don't get why this is showing... The "username_db" var in the query is the same as the one that I have in my DB.



In the same file as the function above, I have this next function and when called, it does fine



public function RegisterUser($user, $pwd, $mail)
$u_Insert = $user;
$p_Insert = $pwd;
$m_Insert = $mail;

$stmt = $this->Connect()->prepare("INSERT INTO user_secure(username_db, password_db) VALUES (?,?)");
$query1 = $stmt->execute([$u_Insert, $p_Insert]);

$stmt = $this->Connect()->prepare("INSERT INTO user_info(mail_db) VALUES (?)");
$query2 = $stmt->execute([$m_Insert]);

if($query2 && $query1)
return true;
else
return false;




Is there something that I'm missing?
I have already checked this thread but I'm still in the exact position...



Thank you for your time



(I'm still learning PDO, sorry if my code isn't clean)







php class session pdo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 3:02







Alan Maldonado

















asked Nov 16 '18 at 2:38









Alan MaldonadoAlan Maldonado

158




158












  • What? Which one?

    – Alan Maldonado
    Nov 16 '18 at 2:58











  • the question has 5 tags; one being jquery.

    – Funk Forty Niner
    Nov 16 '18 at 2:59











  • My bad! Tried to put the tag "query"

    – Alan Maldonado
    Nov 16 '18 at 3:02











  • Ah I see, I understand now.

    – Funk Forty Niner
    Nov 16 '18 at 3:28

















  • What? Which one?

    – Alan Maldonado
    Nov 16 '18 at 2:58











  • the question has 5 tags; one being jquery.

    – Funk Forty Niner
    Nov 16 '18 at 2:59











  • My bad! Tried to put the tag "query"

    – Alan Maldonado
    Nov 16 '18 at 3:02











  • Ah I see, I understand now.

    – Funk Forty Niner
    Nov 16 '18 at 3:28
















What? Which one?

– Alan Maldonado
Nov 16 '18 at 2:58





What? Which one?

– Alan Maldonado
Nov 16 '18 at 2:58













the question has 5 tags; one being jquery.

– Funk Forty Niner
Nov 16 '18 at 2:59





the question has 5 tags; one being jquery.

– Funk Forty Niner
Nov 16 '18 at 2:59













My bad! Tried to put the tag "query"

– Alan Maldonado
Nov 16 '18 at 3:02





My bad! Tried to put the tag "query"

– Alan Maldonado
Nov 16 '18 at 3:02













Ah I see, I understand now.

– Funk Forty Niner
Nov 16 '18 at 3:28





Ah I see, I understand now.

– Funk Forty Niner
Nov 16 '18 at 3:28












1 Answer
1






active

oldest

votes


















5














This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.



$query1 = $stmt->execute([$userToGet]);


In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.



 foreach ($query1->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;




To get the row, you need to write it like this:



$results = $stmt->fetch();


or in your case:



 foreach ( $stmt->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;







share|improve this answer

























  • Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

    – Alan Maldonado
    Nov 16 '18 at 3:05










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%2f53330657%2fphp-pdo-call-to-a-member-function-fetch-on-boolean%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.



$query1 = $stmt->execute([$userToGet]);


In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.



 foreach ($query1->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;




To get the row, you need to write it like this:



$results = $stmt->fetch();


or in your case:



 foreach ( $stmt->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;







share|improve this answer

























  • Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

    – Alan Maldonado
    Nov 16 '18 at 3:05















5














This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.



$query1 = $stmt->execute([$userToGet]);


In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.



 foreach ($query1->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;




To get the row, you need to write it like this:



$results = $stmt->fetch();


or in your case:



 foreach ( $stmt->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;







share|improve this answer

























  • Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

    – Alan Maldonado
    Nov 16 '18 at 3:05













5












5








5







This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.



$query1 = $stmt->execute([$userToGet]);


In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.



 foreach ($query1->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;




To get the row, you need to write it like this:



$results = $stmt->fetch();


or in your case:



 foreach ( $stmt->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;







share|improve this answer















This line here is one of the reasons. execute returns true or false indicating if the query succeeded or failed.



$query1 = $stmt->execute([$userToGet]);


In a sense, $query1 is a boolean.
Now in these lines, you are trying to access the fetch method from $query1 which is a boolean.



 foreach ($query1->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;




To get the row, you need to write it like this:



$results = $stmt->fetch();


or in your case:



 foreach ( $stmt->fetch() as $row) 
if($row['admin_db'] == 1)
return true;
else
return false;








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 2:55









Funk Forty Niner

1




1










answered Nov 16 '18 at 2:49









keyslkeysl

852713




852713












  • Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

    – Alan Maldonado
    Nov 16 '18 at 3:05

















  • Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

    – Alan Maldonado
    Nov 16 '18 at 3:05
















Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

– Alan Maldonado
Nov 16 '18 at 3:05





Thank you!, this solved the main problem, I'll write down this one so I don't make more mistakes :)

– Alan Maldonado
Nov 16 '18 at 3:05



















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%2f53330657%2fphp-pdo-call-to-a-member-function-fetch-on-boolean%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

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric