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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:58:09+00:00 2026-05-27T10:58:09+00:00

Until recently I’ve been using mysql_real_escape_string() to fix most of my variables before making

  • 0

Until recently I’ve been using mysql_real_escape_string() to fix most of my variables before making SQL queries to my database. A friend said that I should be using PDO’s prepared statements instead, so after reading a bit about them I’m now switching over to them.

I’ve only encountered one problem so far in switching over, and that’s counting the rows to returned by a SELECT statement. On occasion in my code, I’d run an SQL query and then count the number of rows returned from the SELECT statement. Depending on whether a result set returned, I would take different actions. Sometimes I do need to use the result set from it. MySQL let me go straight to mysql_fetch_assoc() after mysql_num_rows() with no problem. However, PDO doesn’t seem to have anything like mysql_num_rows().

I’ve been reading some responses on SO that gave me a solution, to either use COUNT() in the SQL statement or to use the PHP function count() on the result set. COUNT() would work fine in the SQL statement if I didn’t need the result set in some places, however, several people have mentioned that using count() on the result set is fairly inefficient.

So my question is, how should I be doing this if I need to count the number of rows selected (if any), then run a script with the result set? Is using count() on the result set the only way in this case, or is there a more efficient way to do things?

Below is a short example of something similar to my previous SQL code:

$query=mysql_query('SELECT ID FROM Table WHERE Name='Paul' LIMIT 1);

if(mysql_num_rows($query)>0)
{
  print_r(mysql_fetch_assoc($query));
}
else
{
  //Other code.
}

Thanks.

EDIT

I do know that you use fetchAll() on the statement before counting the result set (which gives me what I need), but I’m just trying to figure out the most efficient way to do things.

  • 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-05-27T10:58:09+00:00Added an answer on May 27, 2026 at 10:58 am
    $stmt->rowCount();
    

    http://php.net/manual/en/pdostatement.rowcount.php

    the rows must be fetched(buffered into memory, or iterated) for it to work. It’s not uncommon for your pdo driver to be configured to do this automatically.

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

Sidebar

Related Questions

I'd been happy using my debug statements until recently I upgraded Xcode and now
Until recently I have been using cairngorm as a framework for flex. However, in
Up until recently, I had been using Safari 4 for testing and debugging my
I've been using jQuery and YUI side-by-side with no issues until recently. Occasionally, inside
Until recently I'd been using style=display:none on an image to do this with its
I've been using MyISAM and not defining explicit foreign key relationships until recently when
We have a system built using WPF and have (until recently) been developing using
I've been using a vanilla apache install until recently with no major problems. Today
Up until recently, I've been storing multiple values into different hashes with the same
I had until recently been under the impression that the CDbl(x) operation in VB.NET

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.