Jump to content

count how many times the same username is in the database


CBG

Recommended Posts

Hi,

 

I currently have the below code.

 

$query = "select * from db WHERE username LIKE '$username'";
$result = mysql_query($query);
while ($r = mysql_fetch_array($result)) {
$user = $r["username"];
$user_seen = $r["username_seen"];
}

 

Now what I would like to do is count and return the number how many times the same username is in the database, as well as return username

 

How would I do this with the above code?

Link to comment
Share on other sites

Use a SELECT COUNT() query when you want to do something like this.

 

$query = "SELECT COUNT(*) AS num FROM db WHERE username = '$username'";
$result = mysql_query($query);

$numberOfUsernames = mysql_result($result, 0, 0);

// or

list($numberOfUsernames) = mysql_fetch_row($result);

 

You alreay have the username in $username

 

Link to comment
Share on other sites

Huh? Why would it only return 1 row? There's no limit on the query. It will return all rows that have the username supplied, then mysql_num_rows will give the number of records returned. I've just tried this locally too and it's fine

Link to comment
Share on other sites

$query = "SELECT COUNT(*) AS num FROM db WHERE username = '$username'";

 

Would only ever return 1 row containing the count of the number of users that have $username as there name. Did you read my reply?

Link to comment
Share on other sites

I would have assumed that since I said "instead" the OP would realise he should keep his original query since trying to get the number twice would be pretty stupid

 

You didn't mention the word 'instead' (not that that would have made your post any clearer) anywhere in your post, nor did you bother to read mine obviously.

Link to comment
Share on other sites

Well theres really no need to be pedantic about it. No I didn't re-read my post. That said, it was still pretty obvious that I meant use that instead so I've copied and pasted the two words that define it just in case you need the exact words

"just use"

better?

Link to comment
Share on other sites

Use a SELECT COUNT() query when you want to do something like this.

 

$query = "SELECT COUNT(*) AS num FROM db WHERE username = '$username'";
$result = mysql_query($query);

$numberOfUsernames = mysql_result($result, 0, 0);

// or

list($numberOfUsernames) = mysql_fetch_row($result);

 

You alreay have the username in $username

 

 

The while makes me believe he wants to know how many times each username occurs which leads to:

 

SELECT count(*) AS username_count, username
FROM db
GROUP BY username

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.