max_
max_

Reputation: 24481

Get number of items in MySQL database

I am using php to create a highscore database for my iPhone Application. However it will only show 100 highscores (that I set). How can I change this to get the count of all of the rows in the database?

Here is part of my code:

$table = "highscores";

// Initialization
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
mysql_select_db(DB_NAME, $conn);

// Error checking
if(!$conn) {
    die('Could not connect ' . mysql_error());
}

$type   = isset($_GET['type']) ? $_GET['type'] : "global";
$offset = isset($_GET['offset']) ? $_GET['offset'] : "0";
$count  = isset($_GET['count']) ? $_GET['count'] : "100";
$sort   = isset($_GET['sort']) ? $_GET['sort'] : "score DESC";

Upvotes: 1

Views: 6878

Answers (3)

sandalian
sandalian

Reputation: 46

To get the number of records from a table, you can try following code:

<?php
$query = mysql_query("select count(*) as total from table_name");
$result = mysql_fetch_array($query);
echo $result['total'];
?>

Upvotes: 3

DoctorLouie
DoctorLouie

Reputation: 2674

Here's a good article for Getting MySQL Table Size with PHP

Hope that helps.

Upvotes: 0

Seamus Campbell
Seamus Campbell

Reputation: 17906

SELECT COUNT(*) FROM highscores will give you the number of rows in the highscores table. But you would also get all the rows, if that's what you want to do, by eliminating the LIMIT clause from your query. SQL defaults to giving you all of the rows that match.

Upvotes: 1

Related Questions