Jump to content

Recommended Posts

I have a table with an auto_increment column. I\'d like to be able to find the highest number there (basically, I need to display the total number of records in a table) and put that number into a variable.

 

Is there a way to do this?

 

Basically:

 

ID    Name

1     Thomas

2     Chris

3     Jonathan

 

and have \"3\" put into $totalrecords

Link to comment
https://forums.phpfreaks.com/topic/1117-how-to-display-total-of-records/
Share on other sites

See the SQL functions SUM() and/or COUNT()

 

SELECT SUM(ID) AS totalrecords FROM table

 

Then access totalrecords from the mysql_fetch_assoc() function

 

[php:1:826a0f1673]<?php

$conn = mysql_connect(\'dbhost\', \'dbuser\', \'dbpass\') or die(mysql_error());

mysql_select_db(\'dbname\', $conn) or die(mysql_error());

$query = \"SELECT SUM(ID) AS totalrecords FROM table\";

$result = mysql_query($query, $conn) or die(mysql_error());

if (mysql_num_rows($result) > 0) {

$row = mysql_fetch_assoc($result) or die(mysql_error());

$totalrecords = $row[\'totalrecords\'];

echo \'Total Records \' . $totalrecords;

}

mysql_close($conn) or die(mysql_error());

?>[/php:1:826a0f1673]

If I\'m not mistaken this will give you the sum of the ID field which is 1 + 2 + 3 = 6. I think you should use the COUNT function. Change this line:

    $query = "SELECT SUM(ID) AS totalrecords FROM table"; 

To this:

    $query = "SELECT COUNT(*) AS totalrecords FROM table"; 

Or you could simplify it using the mysql_num_rows function like this:

[php:1:9228aa23a2]<?php

$conn = mysql_connect(\'dbhost\', \'dbuser\', \'dbpass\') or die(mysql_error());

mysql_select_db(\'dbname\', $conn) or die(mysql_error());

$query = \"SELECT * FROM table\";

$result = mysql_query($query, $conn) or die(mysql_error());

$totalrecords = mysql_num_rows($result);

echo \'Total Records \' . $totalrecords;

mysql_close($conn) or die(mysql_error());

?>[/php:1:9228aa23a2]

Right in this case use COUNT() rather than SUM(), however as long as you have an AUTO_INCREMENT field asterisks are NOT needed. Also note that COUNT(expr) returns a count of the number of non-NULL values in the rows retrieved by a SELECT statement, while COUNT(*) returns a count of the number of rows retrieved, whether or not they contain NULL values. Do not be tempted to use mysql_num_rows(), using COUNT(expr) or COUNT(*) is a lot faster.

 

[php:1:3b0788305a]<?php

$conn = mysql_connect(\'dbhost\', \'dbuser\', \'dbpass\') or die(mysql_error());

mysql_select_db(\'dbname\', $conn) or die(mysql_error());

$query = \"SELECT COUNT(ID) AS totalrecords FROM table\";

$result = mysql_query($query, $conn) or die(mysql_error());

$row = mysql_fetch_assoc($result) or die(mysql_error());

$totalrecords = $row[\'totalrecords\'];

echo \'Total Records \' . $totalrecords;

mysql_close($conn) or die(mysql_error());

?>[/php:1:3b0788305a]

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.