如何在PHP中获取select count(*)查询的结果?

时间:2021-09-22 09:31:04

I have this query to use in PHP:

我有这个在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?

当我使用echo打印出结果时,没有任何内容被打印出来。上述陈述的返回值究竟是什么?

10 个解决方案

#1


13  

Your code doesn't include any fetch statement. And as another answer notes, you need single quotes around $_SESSION["username"].

您的代码不包含任何fetch语句。正如另一个答案所指出的,你需要围绕$ _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);

#2


10  

mysql_query returns a result resource. You can read the result with mysql_result

mysql_query返回结果资源。您可以使用mysql_result读取结果

$res = mysql_query("select count(*) from registeredUsers where email='".mysql_real_escape_string($_SESSION["username"])."'");
echo mysql_result($res,0);

#3


2  

You need single quotes around the session variable in your query

在查询中,您需要在会话变量周围使用单引号

$result = mysql_query("SELECT COUNT(*) 
                       FROM registeredUsers 
                       WHERE email = '".$_SESSION['username']."' ");

#4


0  

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.

计数查询将始终返回一个值,如果没有返回记录,则返回0;如果记录匹配,则返回大于0的整数。

It should at least be printing out 0, the query you posted means:

它应至少打印0,您发布的查询意味着:

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?

这可能没有意义,你的意思是在用户名=“。$ _ SESSION [”用户名“]或类似的地方吗?

#5


0  

You may want to echo out the query itself to determine that it is returning what you expect.

您可能希望回显查询本身以确定它正在返回您期望的内容。

#6


0  

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.

mysql_query()返回用于从结果集中获取信息的资源。使用mysql_fetch_array()等函数从结果集中检索行。在这种情况下,只会有一行。

#7


0  

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

它应该为您提供具有您提供的电子邮件地址作为此查询的参数的注册用户的数量。 (可以检查给定的电子邮件地址是否已经为其他用户注册。)如果电子邮件地址尚未注册,则返回空字段。 (这可能就是为什么在您的情况下没有打印出来的原因。请使用您确定要在数据库中的电子邮件地址进行尝试。)

#8


0  

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

#9


0  

You need to use mysql_fetch_array() to return value in a user defined variable. Then have to print the returned value.

您需要使用mysql_fetch_array()在用户定义的变量中返回值。然后必须打印返回的值。

 $result=mysql_query("select count(*) from registeredUsers where email='{$_SESSION['username']}'")
 $COUNT_NUMBER=mysql_fetch_array($result); 
 echo "<br>1.Count=" .$COUNT_NUMBER[0]; 

#10


-2  

Try casting it to string before echoing it. As an int, 0 will display as an empty string.

尝试将其转换为字符串,然后再回显它。作为int,0将显示为空字符串。

#1


13  

Your code doesn't include any fetch statement. And as another answer notes, you need single quotes around $_SESSION["username"].

您的代码不包含任何fetch语句。正如另一个答案所指出的,你需要围绕$ _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);

#2


10  

mysql_query returns a result resource. You can read the result with mysql_result

mysql_query返回结果资源。您可以使用mysql_result读取结果

$res = mysql_query("select count(*) from registeredUsers where email='".mysql_real_escape_string($_SESSION["username"])."'");
echo mysql_result($res,0);

#3


2  

You need single quotes around the session variable in your query

在查询中,您需要在会话变量周围使用单引号

$result = mysql_query("SELECT COUNT(*) 
                       FROM registeredUsers 
                       WHERE email = '".$_SESSION['username']."' ");

#4


0  

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.

计数查询将始终返回一个值,如果没有返回记录,则返回0;如果记录匹配,则返回大于0的整数。

It should at least be printing out 0, the query you posted means:

它应至少打印0,您发布的查询意味着:

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?

这可能没有意义,你的意思是在用户名=“。$ _ SESSION [”用户名“]或类似的地方吗?

#5


0  

You may want to echo out the query itself to determine that it is returning what you expect.

您可能希望回显查询本身以确定它正在返回您期望的内容。

#6


0  

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.

mysql_query()返回用于从结果集中获取信息的资源。使用mysql_fetch_array()等函数从结果集中检索行。在这种情况下,只会有一行。

#7


0  

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

它应该为您提供具有您提供的电子邮件地址作为此查询的参数的注册用户的数量。 (可以检查给定的电子邮件地址是否已经为其他用户注册。)如果电子邮件地址尚未注册,则返回空字段。 (这可能就是为什么在您的情况下没有打印出来的原因。请使用您确定要在数据库中的电子邮件地址进行尝试。)

#8


0  

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

#9


0  

You need to use mysql_fetch_array() to return value in a user defined variable. Then have to print the returned value.

您需要使用mysql_fetch_array()在用户定义的变量中返回值。然后必须打印返回的值。

 $result=mysql_query("select count(*) from registeredUsers where email='{$_SESSION['username']}'")
 $COUNT_NUMBER=mysql_fetch_array($result); 
 echo "<br>1.Count=" .$COUNT_NUMBER[0]; 

#10


-2  

Try casting it to string before echoing it. As an int, 0 will display as an empty string.

尝试将其转换为字符串,然后再回显它。作为int,0将显示为空字符串。