Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:07:25+00:00 2026-06-14T09:07:25+00:00

I have a PDO statement that for some reason refuses to execute properly. It

  • 0

I have a PDO statement that for some reason refuses to execute properly. It returns false. However, when running the exact same statement using PDO’s query(), it works like it should . . .

This is the relevant code:

// Prepare PDO statement
$getStatus = $dbHandle->prepare("SELECT `active` FROM `teachers`
                                 WHERE `id` = :teacher LIMIT 1;");
$getStatus->setFetchMode(PDO::FETCH_ASSOC);
$getStatus->bindParam(':teacher', $teacher_id, PDO::PARAM_INT);

// This statement retrieved the relevant teacher id;
// or false, if an invalid teacher was specified; in this
// case, we specify a valid teacher
$teacher_id = array_search($_POST['teacherName'], $acronyms);

// For debugging: show $teacher_id
echo("$teacher_id<br />");

// Execute query; should return an associative array
$newStatusArray = $getStatus->fetch();

// Nothing . . .
print_r($newStatusArray);

// returns "bool(false):
var_dump($newStatusArray);

// Trying with a query
foreach ($dbHandle->query("SELECT `active` FROM `teachers`
                           WHERE `id` = $teacher_id LIMIT 1;") as $row) {
  // This loop runs only once because of the LIMIT 1
  $newStatus = $row['active'];
}

// This prints the new status (was changed before, is always 0 or 1)
echo("$newStatus<br />");

The normal query, although it should do exactly what the prepared statement does, works; but the prepared statement returns nothing . . .

$getStatus->errorCode() is empty . . . So MySQL is happy. Also, $getStatus->debugDumpParams() shows what it’s supposed to:

SQL: [62] SELECT `active` FROM `teachers` WHERE `id` = :teacher LIMIT 1;
Params: 1 Key: Name: [8] :teacher paramno=-1 name=[8] ":teacher"
is_param=1 param_type=1 

I have been trying to get this to work for over an hour now, but I seriously don’t get what’s going wrong. Can anybody see what’s the problem here?

I would greatly appreciate any pointers!

/////////////////////////////////////////////// EDIT: ///////////////////////////////////////////////

Thank you for your answers! So stupid – indeed, I had forgotten the execute statement. Duh . . . Very, very embarassed. Sorry for having taken up your time with this!!! Like this, it does work:

if (!$getStatus->execute()) {
  errorHandler("Errorcode: {$getStatus->errorCode()},
                errorinfo: {$getStatus->errorInfo ()}.");
}
$newStatusArray = $getStatus->fetch();
var_dump($newStatusArray);

The bindParam can stay at the top; which is in line with my understanding of the value of this method; you can just designate a variable to a slot in the PDO statement, and the whenever the query is executed, the value of the variable at that moment is passed along.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:07:26+00:00Added an answer on June 14, 2026 at 9:07 am

    You forgot to actually ->execute() your statement.

    $getStatus->execute(); // added
    $newStatusArray = $getStatus->fetch();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PDO Query that returns what appears to be an array $pds.
I have some working code that uses a SQL select statement within it. As
Basically I have a piece of code that has a switch statement and some
I have a few classes that perform some MySQL queries and prepared statements. However,
I have heard that using PREPARE and EXECUTE in a SQL statement will sanitize
I'm a newcomer to PDO and have to say that I like it so
I am running problems in implementing LIKE in PDO I have this query: $query
I have an issue with PDO that I'd really like to get an answer
I have found many ways to use the exec statement for PDO, but I'm
I have a PDO prepared statement in which the bound variables are prepared dynamically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.