简体   繁体   English

PHP动态计数功能

[英]PHP Dynamic Count Function

I am trying to make ONE dynamic function for count in mysql: 我正在尝试使一个动态函数在mysql中计数:

functions.php: functions.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 Code: HTML代码:

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

But still got blank page without any error!!! 但是仍然有空白页,没有任何错误!!!

You can try this out. 您可以尝试一下。

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

Here you give an alias to the count(*) and use that to access the returned result as $total['count']. 在这里,您给count(*)赋予别名,并使用该别名以$ total ['count']的形式访问返回的结果。 Hope it helps. 希望能帮助到你。

First things you forgot to close else past,second just add this line " ini_set("display_errors", 1); " at the top of your php.this will shows the error in your php. 首先,您忘了关闭其他东西,第二,只需在php的顶部添加以下行“ ini_set(“ display_errors”,1); ”,即可显示php中的错误。

Your code: 您的代码:

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

my code: 我的代码:

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

Thanks guys, Its working well now: 谢谢大家,它现在运行良好:

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 = mysql_query($q);
    $total = mysql_fetch_array($record);
    return $total[0];
}


echo countEntries('news', "type", "sport");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM