开发者

Get number of items in MySQL database

开发者 https://www.devze.com 2023-02-07 04:39 出处:网络
I am using php to create a highscore database for my iPhone Application. However it will 开发者_C百科only show 100 highscores (that I set). How can I change this to get the count of all of the rows in

I am using php to create a highscore database for my iPhone Application. However it will 开发者_C百科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";


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'];
?>


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.


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

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消