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 8422789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:33:15+00:00 2026-06-10T03:33:15+00:00

Using PHP v. 5.2.14 and PDO-MySQL extension. I am new to prepared statements. Need

  • 0

Using PHP v. 5.2.14 and PDO-MySQL extension.

I am new to prepared statements. Need to create a search form (so user input) and a working query of the “Select all X where X like…” variety.

Code and Results:

$sql = 'SELECT COUNT(*) as num_books from t_books where title LIKE :search_term';
// Later access as num_books
$prep = $dbh->prepare($sql);
$num = $prep->execute(array(':search_term' => '%'.$search_term. '%'));
$total=$num->fetchColumn();

Var dump of $prep:
object(PDOStatement)#2 (1) { [“queryString”]=> string(58) “SELECT COUNT(*) from t_books where title LIKE :search_term” }

Fatal error: Call to a member function fetchColumn() on a non-object

If $prep is an object, then $num should be an object. But it is boolean (true). What happened here?

But more importantly, how can I do a count?

I read Row count with PDO -. Suggestions there do not work for me because I need a Prepared Statement. Will have user input.

Thanks for your help.

  • 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-10T03:33:17+00:00Added an answer on June 10, 2026 at 3:33 am

    PDOStatement::execute() only returns a boolean indicating success or failure, so your variable $num is not what you want to fetch from. By assigning the result to $num, you are not assigning the object $prep, but rather only the return value of the method call to execute() (TRUE).

    Instead, fetchColumn() from $prep, your PDOStatement object.

    $num = $prep->execute(array(':search_term' => '%'.$search_term. '%'));
    if ($num) {
      $total = $prep->fetchColumn();
    }
    

    Beyond that, your SQL should execute correctly and the one column it returns should contain the count you need.

    In this case (especially since you are calling fetchColumn() with no argument) it doesn’t matter, but for future readers, it is advisable to name the COUNT() aggregate with an alias that can then be used in an associative or object fetch:

    $sql = 'SELECT COUNT(*) AS num_books from t_books where title LIKE :search_term';
    // Later access as num_books
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this php code: try{ $dbh = new PDO(mysql:host=$host;dbname=$dbname,$user,$pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //
Possible Duplicate: PDO Prepared Statements I'm using the mysqli extension in PHP and I'm
Im using MySQL with PDO PHP scripts to maintain a user database and a
I am using PDO with PHP to create a new database and then a
Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a
I'm using PDO to insert a record (mysql and php) $stmt->bindParam(':field1', $field1, PDO::PARAM_STR); $stmt->bindParam(':field2',
I'm using PDO in my PHP application. It connects to a MySQL server on
Using PDO / PHP and MySQL, how can I check if a record was
I'm using the following php pdo code to insert data into mysql database, the
I'm using WAMP on windows, which installs PHP, Apache and MySQL. I'm now working

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.