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

我需要在我的 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');

?>


POPMUISE
浏览 127回答 2
2回答

慕姐8265434

您应该在新的 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.";&nbsp; $content = '<div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-3 col-xs-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- small box -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="small-box bg-aqua">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="inner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<h3><?php echo "$rows"; } ?></h3>*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Doctors</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="icon">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="ion ion-bag"></i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://localhost/medibed/doctor" class="small-box-footer">View Doctors <i class="fa fa-arrow-circle-right"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <!-- ./col -->&nbsp; &nbsp; &nbsp; </div>';&nbsp; 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.";&nbsp; $content = '<div class="row">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg-3 col-xs-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- small box -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="small-box bg-aqua">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="inner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<h3>'.$rows.'</h3>*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Doctors</p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="icon">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="ion ion-bag"></i>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://localhost/medibed/doctor" class="small-box-footer">View Doctors <i class="fa fa-arrow-circle-right"></i></a>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- ./col -->&nbsp; </div>';include('../master.php');?>
打开App,查看更多内容
随时随地看视频慕课网APP