counting records in a database
Moderator: General Moderators
counting records in a database
I just want to count how many records are in a table but can't seem to find anything on it.
think i got it
Code: Select all
<?php
$db_name = " ";
$table_name = " ";
$connection = @mysql_connect("localhost", "username", "password")
or die("Couldn't connect.");
$db = @mysql_select_db($db_name, $connection)
or die("Couldn't select database.");
$query = "SELECT COUNT(*) FROM $table_name";
$count = mysql_query($query) or die("Select Failed!");
$count = mysql_fetch_array($count);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<? echo $count[0]; ?>
</body>
</html>
?>Code: Select all
<?php
$query = "SELECT COUNT(someFieldName) as rowCount FROM $table_name";
$count = mysql_query($query) or die("Select Failed!");
$rowCount = mysql_fetch_array($count);
echo "total rows in table are ".$rowCount['rowCount'];
?>- mudkicker
- Forum Contributor
- Posts: 479
- Joined: Wed Jul 09, 2003 6:11 pm
- Location: Istanbul, TR
- Contact:
Code: Select all
<?php
$rowCount = mysql_fetch_array($count);
// this is even better -----v
$rowCount = mysql_fetch_assoc($count);
?>