MySQL 计数功能无法正常工作

发布于 2024-11-28 21:28:58 字数 848 浏览 3 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月染过的梦 2024-12-05 21:28:58

假设 $gravatar = md5(strtolower(trim("$email"))); 与此处的问题无关且不需要,您还可以使用一个数据库查询来获取数数:

$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_count_query = 
    "SELECT COUNT(*)
     FROM likes 
     WHERE username = 
           ( SELECT email
             FROM users 
             WHERE uname = '$req_user'
           )
    ";
$result = mysql_query($get_count_query) or die(mysql_error());
$row = mysql_fetch_row($result);
$user_likes = $row[0];

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:

$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_count_query = 
    "SELECT COUNT(*)
     FROM likes 
     WHERE username = 
           ( SELECT email
             FROM users 
             WHERE uname = '$req_user'
           )
    ";
$result = mysql_query($get_count_query) or die(mysql_error());
$row = mysql_fetch_row($result);
$user_likes = $row[0];
聆听风音 2024-12-05 21:28:58

你没有获取它...

这样做:

$result = mysql_query("SELECT COUNT(*) FROM likes WHERE username = '$email'");

$user_likes = mysql_fetch_row($result);

You didn't fetch it...

Do it like this:

$result = mysql_query("SELECT COUNT(*) FROM likes WHERE username = '$email'");

$user_likes = mysql_fetch_row($result);
jJeQQOZ5 2024-12-05 21:28:58

首先,您需要正确比较电子邮件(不区分大小写):

"... WHERE UPPER(username) = '" . strtoupper($email) . "'"

然后 $user_likes 是一个资源变量,它不会直接给您计数。

您需要先获取它:

$row = mysql_fetch_row($user_likes);
echo 'Count: ' . $row[0]; 

First you need to properly compare the emails (case insensitive) :

"... WHERE UPPER(username) = '" . strtoupper($email) . "'"

Then $user_likes is a resource variable, it won't give you the count directly.

You need to fetch it first :

$row = mysql_fetch_row($user_likes);
echo 'Count: ' . $row[0]; 
傾旎 2024-12-05 21:28:58

你应该这样做:

$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());
if ($rows = mysql_fetch_assoc($result)) {
    $email = $rows['email'];
    $gravatar = md5(strtolower(trim("$email")));
    $get_data = "SELECT COUNT(*) FROM likes WHERE username = '$email'";
    $user_likes = mysql_query($get_data) or die(mysql_error());
    if ($row = mysql_fetch_row($user_likes)) {
       $nbr = 1*$row[0]; 
    } else {
       // it could never happen ;-)
       $nbr = 0;
    }
} else {
    // no match with this user!
    $nbr = 0;
}
echo "This user likes $nbr times !";

You should do that :

$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());
if ($rows = mysql_fetch_assoc($result)) {
    $email = $rows['email'];
    $gravatar = md5(strtolower(trim("$email")));
    $get_data = "SELECT COUNT(*) FROM likes WHERE username = '$email'";
    $user_likes = mysql_query($get_data) or die(mysql_error());
    if ($row = mysql_fetch_row($user_likes)) {
       $nbr = 1*$row[0]; 
    } else {
       // it could never happen ;-)
       $nbr = 0;
    }
} else {
    // no match with this user!
    $nbr = 0;
}
echo "This user likes $nbr times !";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文