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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:28:30+00:00 2026-06-17T06:28:30+00:00

My code: $pdo = new PDO(‘mysql:host=localhost; dbname=test’, ‘root’, ‘password’); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec(‘SET NAMES utf8’);

  • 0

My code:

$pdo = new PDO('mysql:host=localhost; dbname=test', 'root', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
...
$count = $pdo->query('SELECT COUNT(*) FROM myTable');

I’m trying to count all records in myTable and store that number as $count. How can I make this happen?

I get this message: Object of class PDOStatement could not be converted to int. To my understanding, it is not an int.

  • 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-17T06:28:31+00:00Added an answer on June 17, 2026 at 6:28 am

    Actually, that query will return a PDO statement. Then you can use that statement to get the item of interest.

    $statement = $pdo->query('SELECT COUNT(*) FROM myTable');
    $result = $statement->fetch();
    $count = $result[0];
    

    This gets the statement from your query, then gets the first row (the only row from this query) and then stores the count, $result[0], into a variable named $count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm testing with this code: <?php $db = new PDO('mysql:host=localhost;dbname=test', 'root', 'root'); if(filter_has_var(INPUT_POST, 'submit'))
try{ $pdo = new PDO(mysql:host=localhost;dbname=name, 'user', 'pass'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $statement = SELECT id FROM
function comp_post_code($comp_post_code){ global $host, $dbname, $user, $pass; $DBH = new PDO(mysql:host=$host;dbname=$dbname, $user, $pass); $DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
Using this php code: try{ $dbh = new PDO(mysql:host=$host;dbname=$dbname,$user,$pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //
//this is my connection.php <?php $pdo = new PDO('mysql:host=localhost;dbname=test;', 'root'); $pdo->exec(SET CHARACTER SET utf8);
I have the following code: $link = new PDO(mysql:dbname=$databasename;host=127.0.0.1,$username,$password); $query = $link->prepare(SELECT * FROM
Here is my code: function connect() { $pdo = new PDO(mysql:host=localhost;dbname=www,user,pass); $xmax = SELECT
I currently have the following code: $dbh = new PDO('mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
<?php try { $db = new PDO(mysql:host=localhost;dbname=DBNAME, USER, PASSWD); $stmt = $db->prepare(SELECT id, name
$db = new PDO(mysql:host=$hostname;dbname=$database,$username,$password); $menus = 'SELECT menus FROM menus'; foreach($db->query($menus) as $row ){

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.