Page 1 of 1

counting records in a database

Posted: Thu Oct 21, 2004 11:11 pm
by hward
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

Posted: Thu Oct 21, 2004 11:22 pm
by hward

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>
?>

Posted: Fri Oct 22, 2004 3:12 am
by phpScott

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

Posted: Fri Oct 22, 2004 4:31 am
by m3mn0n
You could also just run a normal query and use [php_man]mysql_num_rows()[/php_man].

Posted: Fri Oct 22, 2004 6:19 am
by mudkicker

Code: Select all

<?php
$rowCount = mysql_fetch_array($count);
// this is even better -----v
$rowCount = mysql_fetch_assoc($count);
?>