Jump to content

COUNT()


The Little Guy

Recommended Posts

What I figured out is that id is a individual number, that is why I am getting that.

 

SO...

 

New approach...

 

Is this the only way to count the total number of rows in a table?

 

SELECT id FROM tablename

 

then in the php use mysql_num_rows

 

OR, is there a better way, one that doesn't select hundreds of rows in one query?

Link to comment
Share on other sites

If you simply want the total number of rows in a table, you don't have to group by anything. Just select the count of the id column:

SELECT COUNT(id) AS count FROM tablename;

 

Then, you can retrieve the result:

<?php
$sql = mysql_query("SELECT COUNT(id) AS count FROM tablename");
$count = mysql_result($sql, 0, 'count');
?>

Link to comment
Share on other sites

I found this, does it work as well?

 

SHOW TABLE STATUS FROM murdercup

 

I can narrow it down to a specific table if I want using:

 

SHOW TABLE STATUS FROM murdercup LIKE 'users'

 

I would like to use the first one:

SHOW TABLE STATUS FROM murdercup

 

because it shows more info in one query.

 

but then I need to somehow return the results. So if I go this method, is it possible to select a certain row without using numbers? I was hoping like this, but it doesn't work:

$sql = mysql_query("SHOW TABLE STATUS FROM murdercup");
$row = mysql_fetch_array($sql);

echo $row['Name']=>['users']['Rows'];

the above I was hoping that it would grab column1 "Name", search down it for the row labeled "users" then get the info from row "Rows", and it would return 41

Link to comment
Share on other sites

You can use anything that will give you the right data, but keep in mind, depending on your server setup, you may eventually be working with a server that only has raw writing privileges (INSERT, UPDATE, SELECT, DELETE), so you'll need to be sure you understand how to get the data you're after any way possible, too ;)

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.