Im trying to count the number of rows in a MySQL database and report that number using PHP. I’ve read all the documentation and I think this piece of code should be working but no matter what I try, it fails.
<?php
session_start();
if ( isset($_SESSION['username']) ) {
$username = $_SESSION['username'];
$q = "SELECT * FROM messages WHERE recepiants='$username' AND readcheck='T'";
$count = mysql_num_rows($q);
?>
<div class="user_info">Logged in as <?php echo $username; ?> | <a href="admin.php">Add a post</a> | <a href="experiment.php">View Posts</a> | <a href="inbox.php">Message Center <?php echo $count; ?> </a> | <a href="logout.php">Log Out</a></div>
<?php } else {?>
The script successfully reports $username but $count doesn’t return a number. Any ideas? Am I missing a bracket or semi-colon somewhere?
The thing you’re missing is running the actual query:
Probably better would be to run a
COUNTin the query, if that’s all you need:And, as someone else noted, you need an active database connection.
http://www.php.net/manual/en/function.mysql-fetch-assoc.php