【问题标题】:Display Count of DB Records in PHP Dashboard在 PHP 仪表板中显示数据库记录数
【发布时间】:2020-10-30 00:44:14
【问题描述】:

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

这是我的仪表板页面的 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 sql database count echo


    【解决方案1】:

    你应该在新的 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');
    ?>
    

    【讨论】:

    • 警告:mysqli_query() 至少需要 2 个参数,1 个在 C:\xampp\htdocs\medibed\Dashboard\index.php 第 3 行给出 警告:mysqli_num_rows() 需要参数 1 为 mysqli_result , null 在第 4 行的 C:\xampp\htdocs\medibed\Dashboard\index.php 中给出我的表中有行。
    • 你做了连接吗? $con = mysqli_connect("localhost","my_user","my_password","my_db");
    • 您应该与您的数据库建立连接并在查询中使用它,例如 $con = mysqli_connect("localhost","my_user","my_password","my_db"); $result = mysqli_query($con,"SELECT * FROM 医生");
    • 我现在添加了连接,但仍然得到:警告:mysqli_query() 需要至少 2 个参数,1 在第 4 行的 C:\xampp\htdocs\medibed\Dashboard\index.php 中给出警告: mysqli_num_rows() 期望参数 1 为 mysqli_result,在第 5 行的 C:\xampp\htdocs\medibed\Dashboard\index.php 中给出 null 我的表中有行。
    • 如果您只需要计数,那么为什么不在查询中使用聚合函数 count(*)。这对你有更好的帮助。
    【解决方案2】:

    如果您只需要计数,那么为什么不在查询中使用聚合函数 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');
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2014-11-29
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2012-11-16
      相关资源
      最近更新 更多