在 PHP 仪表板中显示数据库记录数

Display Count of DB Records in PHP Dashboard

我需要在我的 SQL 表“医生”中显示记录(行)的“计数”。此计数必须显示在我的仪表板页面上的以下医生总数元素中

这是我的仪表板页面index.php

    <?php

$con = mysqli_connect("localhost","root","","hospital_db");
$result = mysqli_query($con,"SELECT * FROM doctors");
$rows = mysqli_num_rows($result);

  $content = '<div class="row">
        <div class="col-lg-3 col-xs-6">
          <!-- small box -->
          <div class="small-box bg-aqua">
            <div class="inner">
             <h3><?php echo '.$rows.';?></h3>

              <p>Doctors</p>
            </div>
            <div class="icon">
              <i class="ion ion-bag"></i>
            </div>
            <a href="http://localhost/medibed/doctor" class="small-box-footer">View Doctors <i class="fa fa-arrow-circle-right"></i></a>
          </div>
        </div>
        <!-- ./col -->

      </div>';
  include('../master.php');
?>

你应该在新的 php 版本中使用 mysqli 对象试试下面的代码

先建立连接点赞

<?php

$con = mysqli_connect("localhost","my_user","my_password","my_db");

$result = mysqli_query($con,"SELECT * FROM doctors");
$rows = mysqli_num_rows($result);
echo "There are " . $rows . " rows in my table.";

  $content = '<div class="row">
        <div class="col-lg-3 col-xs-6">
          <!-- small box -->
          <div class="small-box bg-aqua">
            <div class="inner">
              *<h3><?php echo "$rows"; } ?></h3>*

              <p>Doctors</p>
            </div>
            <div class="icon">
              <i class="ion ion-bag"></i>
            </div>
            <a href="http://localhost/medibed/doctor" class="small-box-footer">View Doctors <i class="fa fa-arrow-circle-right"></i></a>
          </div>
        </div>
        <!-- ./col -->

      </div>';
  include('../master.php');
?>

如果您只需要计数,那么为什么不在查询中使用聚合函数 count(*)。这更好地帮助你。在 h3 标签中的代码中,您可以直接连接字符串,而不是再次使用 php 代码。这可能看起来更好并且结构化。

试试这个:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$result = mysqli_query($con,"SELECT count(*) as total_rows FROM doctors");
$rows = $result->total_rows;
echo "There are " . $rows . " rows in my table.";
  $content = '<div class="row">
        <div class="col-lg-3 col-xs-6">
          <!-- small box -->
          <div class="small-box bg-aqua">
            <div class="inner">
          *<h3>'.$rows.'</h3>*
          <p>Doctors</p>
        </div>
        <div class="icon">
          <i class="ion ion-bag"></i>
        </div>
        <a href="http://localhost/medibed/doctor" class="small-box-footer">View Doctors <i class="fa fa-arrow-circle-right"></i></a>
      </div>
    </div>
    <!-- ./col -->
  </div>';
include('../master.php');
?>