user2698141
user2698141

Reputation: 11

How would I be able to track Admin SQL

How can I track only the amount of Administrators from a Database that looks like this:

table: users

username        title
Glenn       Administrator
Peter       New Member

I want to only track the number of Administrators in the database. Here is the code that I am using but I think its missing some code. Here is the code:

// Count number of users
$result = mysql_query("SELECT title FROM users WHERE Administrator"); 
$users_count = mysql_num_rows($result); 
// Display the results 
echo $users_count; 

I think the WHERE is suppose to be wrong. How can I track only the amount count of Administrators where the table is users and the row is title?

Upvotes: 1

Views: 30

Answers (1)

ajtrichards
ajtrichards

Reputation: 30565

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Your query should be:

SELECT * FROM users WHERE title = 'Administrator'

To rewrite your script using mysqli:

$mysqli = new mysqli("localhost", "my_user", "my_password", "database_name");

$sql    = "SELECT * FROM users WHERE title = 'Administrator'";
$result = $mysqli -> query($sql);
$num    = $result -> num_rows;

echo 'Adminstrators found: '.$num;

Upvotes: 3

Related Questions