2
$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

2
  • Has this line $gravatar = md5(strtolower(trim("$email"))); anything to do with the question? Commented Aug 11, 2011 at 16:15
  • No, it has nothing to do with the question, sorry. Commented Aug 11, 2011 at 17:34

4 Answers 4

2

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];
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but the output is literally "Array". using <?php echo $user_likes; ?>
Counting means that you want a number though, or am I missing something?
That's right, I need a number. But instead, the output is "Array" and not a number. # of Likes Posted: Array
Nevermind! After taking a look at php.net/manual/en/function.mysql-fetch-row.php - I changed it to echo $user_likes[0];. It's working fine now - thank you! :)
Yes, you found it. It's an array with one row only and not a number (because of the COUNT(*).)
1

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);

1 Comment

Same as @ypercube 's solution, the output is "Array" and not a number.
1

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]; 

1 Comment

MySQL is case insensitive when using LIKE or = with CHAR fields.
1

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 !";

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.