gpt4 book ai didi

PHP动态计数函数

转载 作者:行者123 更新时间:2023-11-29 23:19:11 25 4
gpt4 key购买 nike

我正在尝试为 mysql 中的计数创建一个动态函数:

函数.php:

  function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total[0];
}

HTML 代码:

<?php echo countEntries("news", "category", "1"); ?>
<?php echo countEntries("post", "type", "Sports"); ?>

但仍然有空白页,没有任何错误!

最佳答案

你可以试试这个。

function countEntries($table, $where = '', $what = '')
{
if (!empty($where) && isset($what)) {
$q = "SELECT COUNT(*) AS count FROM " . $table . " WHERE " . $where . " = '" . $what . "' LIMIT 1";
} else{
$q = "SELECT COUNT(*) AS count FROM " . $table . " LIMIT 1";
}
$record = query($q);
$total = fetchrow($record);
return $total['count'];
}

在这里,您为 count(*) 指定一个别名,并使用它来访问返回的结果 $total['count']。希望对您有所帮助。

关于PHP动态计数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27480358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com