0

So I have this function (below) and it is supposed to get the amount of users in a table that I have. I know there's 5 users and they all have an ID. However whenever I use the function to display it on a webpage it does not return a result.

This is the function

function user_count(){
    mysql_connect("IP", "USER", "PASS");
    mysql_select_db("db");
    return mysql_query("SELECT * FROM users WHERE id>=0");
}

And this is the php for the website

<strong><span data-toggle="counter" data-to="
<?php 
$users = user_count();
while ($row = mysql_fetch_array($users)){
    echo $row['count']; 
}
?>"></span></strong>
1
  • What is "count" in your query? If you want to get the count, you should do "select count(*) as 'count' from users where id >= 0" Commented Jul 9, 2015 at 3:23

1 Answer 1

1

Use mysql_num_rows instead.

    function user_count(){
        mysql_connect("IP", "USER", "PASS");
        mysql_select_db("db");
        $result = mysql_query("SELECT * FROM users WHERE id>=0");
        return mysql_num_rows($result)
    }

And then just echo the value, echo user_count().

Also, I would recommend using mysqli_* functions, as mysql_* is deprecated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.