MySQL 计数功能无法正常工作
$req_user = trim($_GET['user']);
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$get_data = "SELECT * FROM `users` WHERE uname = '$req_user'";
$result = mysql_query($get_data) OR die(mysql_error());
$rows = mysql_fetch_assoc($result);
$email = $rows['email'];
$gravatar = md5(strtolower(trim("$email")));
$user_likes = mysql_query("SELECT COUNT(*) FROM likes WHERE username = '$email'");
我正在尝试计算数据库中喜欢
的所有行以及用户名
字段中当前用户的电子邮件。
(这是一个从 userinfo.php?user=xxx
等链接获取信息的循环。)
当我 echo $user_likes
时没有输出。
代码有什么问题?
http://www.tutorialspoint.com/mysql/mysql-count-function.htm
$req_user = trim($_GET['user']);
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$get_data = "SELECT * FROM `users` WHERE uname = '$req_user'";
$result = mysql_query($get_data) OR die(mysql_error());
$rows = mysql_fetch_assoc($result);
$email = $rows['email'];
$gravatar = md5(strtolower(trim("$email")));
$user_likes = mysql_query("SELECT COUNT(*) FROM likes WHERE username = '$email'");
I'm trying to count all of the rows in the database likes
with the email of the current user in their username
field.
(It's a loop to get info from links like userinfo.php?user=xxx
.)
When I echo $user_likes
there is no output.
What is wrong with the code?
http://www.tutorialspoint.com/mysql/mysql-count-function.htm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设
$gravatar = md5(strtolower(trim("$email")));
与此处的问题无关且不需要,您还可以使用一个数据库查询来获取数数:Assuming that
$gravatar = md5(strtolower(trim("$email")));
is not related to the question here and not needed, you could also use one query to the database, to get the count:你没有获取它...
这样做:
You didn't fetch it...
Do it like this:
首先,您需要正确比较电子邮件(不区分大小写):
然后 $user_likes 是一个资源变量,它不会直接给您计数。
您需要先获取它:
First you need to properly compare the emails (case insensitive) :
Then $user_likes is a resource variable, it won't give you the count directly.
You need to fetch it first :
你应该这样做:
You should do that :