tags:

views:

115

answers:

4
$sql = "SELECT count(u_id) AS num_replies FROM `replies` WHERE `u_id`='".$uid."'";
$res = mysql_query($sql) or die(myqsl_error());

Will that return the number of replies a user with id $uid has made? If not, can anyone suggest something that will?

Thx for the help.

+5  A: 

It difficult to answer this question without knowing more about the replies table, but just looking at your query, then yes your query looks like it will.

Notorious2tall
A: 

$ssql = "SELECT count(*) AS num_replies FROM replies WHERE u_id = $uid";
$rres = mysql_query($ssql);

  • Nicholas
Nicholas Kreidberg
The GROUP BY clause is unnecessary because the WHERE clause reduces the set that COUNT(*) is dealing with to just the records matching $uid.
Welbog
Good point, comment edited. -- Thank you!
Nicholas Kreidberg
So now your query is the same as his except it's counting * instead of u_id which makes absolutely no difference. I'm not sure why you bothered answering.
Welbog
Actually, if u_id is nullable, then it *does* make a difference.
Rob
If it's nullable, then it will be rejected by the WHERE condition.
Welbog
A: 

It will return the number of records in the table with that u_id. Doesn't matter what you put in the paretheses. You don't need any GROUP BY clause.

le dorfier
+1  A: 

Please, for the love of the internet, don't built an SQL query yourself. Use PDO.

Paul Tarjan