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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5758121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:23:54  来源:igfitidea点击:

How to get the result of a select count(*) query in PHP?

phpmysql

提问by user722769

I have this query to use in PHP:

我有这个查询要在 PHP 中使用:

mysql_query("select count(*) from registeredUsers where email=".$_SESSION["username"]);

When I use echoto print out the result, nothing gets printed. What exactly is the return value from the above statement?

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

回答by Michael Berkowski

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

回答by Frank Farmer

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

回答by Brett

You need single quotes around the session variable in your query

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

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

回答by BrandonG

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() 之类的函数从结果集中检索行。在这种情况下,将只有一行。

回答by Tom Gullen

The count query will always return a value, which is 0if no records are returned, or an integer above 0if 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

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?

这可能没有意义,你的意思是做where username = ".$_SESSION["username"]或类似的事情吗?

回答by Donnie

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

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

回答by Teetrinker

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

它应该为您提供具有您提供作为此查询参数的电子邮件地址的注册用户数量。(可能是检查给定的电子邮件地址是否已为其他用户注册。)如果电子邮件地址尚未注册,则将返回一个空字段。(这可能是您的案例中没有打印任何内容的原因。使用您确定在数据库中的电子邮件地址尝试一下。)

回答by Rizwan Ranjha

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

回答by Muhammad Ashikuzzaman

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

回答by alexantd

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

在回显之前尝试将其转换为字符串。作为整数,0 将显示为空字符串。