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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:38:27+00:00 2026-05-25T15:38:27+00:00

Two quick (I hope!) questions: Counting rows Is it more efficient to use COUNT(id)

  • 0

Two quick (I hope!) questions:

Counting rows

Is it more efficient to use COUNT(id) in an SQL query, or to just select all the rows and use PHP’s mysql_num_rows() function on the returned resource? Would this depend on the size of the results query? Is there a fairly easy way of determining the answer to this question?

Results arrays

At the moment, I am using COUNT(id) in a query, and then obviously using mysql_query() and mysql_fetch_array(), as follows:

$sql = "SELECT COUNT(id) FROM TABLE1;";
$resource = mysql_query($sql) or die(mysql_error())
$result = mysql_fetch_array($resource);
$count = $result[0];

and this returns the number of rows fine. However, I’m surprised that

$count = mysql_fetch_array($resource)[0];

doesn’t work; I get the error:

Parse error: syntax error, unexpected '[' in C:\ServerDocs\file1.php on line 44 

is there an easy explanation as to what’s going on?

Thanks in advance,
Chris

  • 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-25T15:38:27+00:00Added an answer on May 25, 2026 at 3:38 pm
    1. It depends on the situation, but SELECT COUNT(*) is normally faster.
      The reason is simply that using mysql_num_rows(), the database
      server has to actually build the result set. With a COUNT(*), on the
      other hand, the DB may be able to determine the number of rows just
      from indexes, which is much faster. So if all you want is the count,
      then mysql_num_rows() ends up doing a lot more work for the same
      result.

      Another down side is that mysql_num_rows doesn’t work with
      unbuffered queries. So if you need to work with very large result
      sets, using mysql_num_rows might not be an option. However, COUNT(*)
      is always available.

    2. $count = mysql_fetch_array($resource)[0];

      This type of statements are not possible in current version of php.
      So, better use

      $no_of_rows = mysql_fetch_array($count);
      echo $no_of_rows[0];
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two quick questions which I just want to get the community's opinions
Two quick questions if I may, is this how I should go about taking
I have two quick questions: When do two file descriptors point to the same
Is there a quick query I can run that returns all columns of the
Two quick questions: What is the maximum size for the name element on an
I have two quick button questions in VS. First, I want to create a
----- PHP and mySQL ----- I have two quick questions need some advice. On
I have two quick, easy questions on C# in Visual Studio. First, is there
A quick Question. Suppose I have the following two queries: SELECT TOP 2 *
I have two quick questions about Ruby's String#crypt(salt) method: In the documentation it says

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.