Jump to content

[SOLVED] Counting Records in MySQL


Albatross

Recommended Posts

Once again I've hit a roadblock...

 

I've been trying to write a page that checks the database and counts the number of records and if the number is less than 3000, it loads page X, but if the number is equal to 3000, it loads page Y.

 

I know this should be a simple if/else statement but it's the checking and counting that I cannot get to work.  Any help would be appreciated.

Link to comment
Share on other sites

You have to check for the number by using either mysql_num_rows or mysql_affected_rows, depending on the action you are doing.

Something like:

<?php $query = mysql_query("SELECT something FROM table");  if (mysql_num_rows($query) < 3000) { something; } else { something; } ?> 

Link to comment
Share on other sites

The problem with pedro's solution is that query has to return 3000 somethings before you can count them (what if the table contains 1,000,000 somethings?)

 

"SELECT COUNT(*) FROM tablename" will return the value instantly as mysql will just lookup the number of recs in the table without having to read any records.

Link to comment
Share on other sites

With no email, no ID field is generated and that's half of what I needed anyway so if the code is skippinng blanks in it's total count, that would be in my favor....unless I needed to total records regardless of content.

 

(I think I understood what I just said...)

 

But thank you for the heads up.  I changed it back to the * and added the not null attribute to the field so it MUST be populated in order to process.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.