Reputation: 177
I have this query to use in PHP:
mysql_query("select count(*) from registeredUsers where email=".$_SESSION["username"]);
When I use echo
to print out the result, nothing gets printed. What exactly is the return value from the above statement?
Upvotes: 11
Views: 60289
Reputation: 3143
You need to use mysql_fetch_array() to return value in a user defined variable. Then have to print the returned value.
$result=mysql_query("select count(*) from registeredUsers where email='{$_SESSION['username']}'")
$COUNT_NUMBER=mysql_fetch_array($result);
echo "<br>1.Count=" .$COUNT_NUMBER[0];
Upvotes: 0
Reputation: 380
$resultemp = mysql_query("select count(*) AS count from registeredUsers where email='{$_SESSION['username']}'");
// Verify mySQL Query Rresult
if (!$resultemp) echo mysql_error();
// Convert mySQL Result for PHP
$counter=mysql_fetch_assoc($resultemp);
$counter=$counter['count'];
// Print Total Employees
echo $counter;
Upvotes: 0
Reputation: 270617
Your code doesn't include any fetch statement. And as another answer notes, you need single quotes around $_SESSION["username"]
.
$result = mysql_query("select count(*) from registeredUsers where email='{$_SESSION['username']}'");
// Verify it worked
if (!$result) echo mysql_error();
$row = mysql_fetch_row($result);
// Should show you an integer result.
print_r($row);
Upvotes: 14
Reputation: 575
It should give you the amount of registere users who have the email address that you provide as the parameter to this query. (Might be a check if the given email address is already registered for another user.) If the email address is not yet registered, an empty field will be returned. (That might be the reason why nothing gets printed out in your case. Try it with an email address that you are certain of to be in the database.)
Upvotes: 0
Reputation: 39356
mysql_query returns a result resource. You can read the result with mysql_result
$res = mysql_query("select count(*) from registeredUsers where email='".mysql_real_escape_string($_SESSION["username"])."'");
echo mysql_result($res,0);
Upvotes: 10
Reputation: 3605
Try casting it to string before echoing it. As an int, 0 will display as an empty string.
Upvotes: -2
Reputation: 367
mysql_query() returns a resource used to get information from the result set. Use a function such as mysql_fetch_array() to retrieve rows from the result set. In this case, there will only be one row.
Upvotes: 0
Reputation: 721
You need single quotes around the session variable in your query
$result = mysql_query("SELECT COUNT(*)
FROM registeredUsers
WHERE email = '".$_SESSION['username']."' ");
Upvotes: 2
Reputation: 6351
You may want to echo out the query itself to determine that it is returning what you expect.
Upvotes: 0
Reputation: 61727
The count query will always return a value, which is 0
if no records are returned, or an integer above 0
if records match it.
It should at least be printing out 0
, the query you posted means:
Get the number of records where the email address is equal to the session username
This might not make sense, do you mean to do where username = ".$_SESSION["username"]
or something similar?
Upvotes: 0