[英]Count registered users in database
I would like to echo the number of people registered on my website only the code that I have does not work, it gives me back that it can't be converted to string.我想回显在我的网站上注册的人数,只有我拥有的代码不起作用,它告诉我它无法转换为字符串。 Also when I make it a function to call in my HTML I get error that $connection is undefined此外,当我将其设置为在 HTML 中调用的函数时,我收到 $connection 未定义的错误
require_once("connect.php");
$sql = "SELECT * FROM persons";
if ($result=mysqli_query($connection, $sql)){
$rowcount = mysqli_num_rows($result);
mysqli_free_result($result);
return $result;}
How do I get this in a function that I can call on my page that prints the number of people registered?如何在我可以在打印注册人数的页面上调用的函数中获取此信息?
First of all you should use count
because of speed issues:首先,由于速度问题,您应该使用count
:
$sql = "SELECT COUNT(id) FROM persons";
To write a function that returns the number, you can do something like要编写一个返回数字的函数,您可以执行以下操作
function registredMemberCount ($connection)
{
$sql = "SELECT COUNT(id) FROM persons";
$result = mysqli_query($connection,$sql);
$rows = mysqli_fetch_row($result);
return $rows[0];
}
and call it with并调用它
registredMemberCount($connection);
require_once("connect.php");
function blah()
{
global $connection;
$sql = "SELECT COUNT(*) FROM persons";
if ($result=mysqli_query($connection, $sql)){
$row= mysqli_fetch_array($result);
$rowcount = $row[0];
mysqli_free_result($result);
}
return $rowcount;
}
echo blah();
Lets see this,来看看这个
require('connect.php');
function total_num_users(){
$sql = "SELECT * FROM persons";
$result = mysqli_query($connection,$sql);
$count = mysqli_num_rows($result);
return $count;
}
And you can call and echo it like this.你可以像这样调用和回显它。
echo total_num_users();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.