显示类中 SQL 的总 id 计数

我正在使用三个文件相互连接:


仪表板.php


<?php

  require_once('dashboard.vc.php');

  echo $lstTotalCoupon['total_coupons'];

?>

仪表板.vc.php


<?php

  $routePath = "../";


  require_once($routePath . "_config/db.php");

    $dbConfig = new config_db();

    $db = $dbConfig->init();


  require_once($routePath . "_mc/Product.mc.php");

  $mcProduct = new Product_MC();


  $lstTotalCoupon = $mcProduct->SelectCountProduct($db);


?>

产品.mc.php


<?php

Class Product_MC {

   public function SelectCountProduct($db) {

    $stmt = $db->prepare(" SELECT * AS total_coupons FROM product ");

    $stmt->execute();

    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

    return $rows;

  }

}

?>

我使用变量 $lstTotalCoupon 从 Product.mc.php 中获取选择计数,但出现错误


注意:未定义索引:total_coupons。


似乎缺少什么?


长风秋雁
浏览 164回答 3
3回答

Qyouu

产品.mc.php<?phpClass Product_MC {&nbsp; &nbsp;public function SelectCountProduct($db) {&nbsp; &nbsp; $stmt = $db->prepare(" SELECT * AS total_coupons FROM product ");&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);&nbsp; &nbsp; return count($rows);&nbsp; }}?>这将返回获取行的总数或者你甚至可以这样设置:<?phpClass Product_MC {&nbsp; &nbsp;public function SelectCountProduct($db) {&nbsp; &nbsp; $stmt = $db->prepare(" SELECT * AS total_coupons FROM product ");&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);&nbsp; &nbsp; return $rows;&nbsp; }}?>仪表板.vc.php<?php&nbsp; $routePath = "../";&nbsp; require_once($routePath . "_config/db.php");&nbsp; &nbsp; $dbConfig = new config_db();&nbsp; &nbsp; $db = $dbConfig->init();&nbsp; require_once($routePath . "_mc/Product.mc.php");&nbsp; $mcProduct = new Product_MC();&nbsp; $lstTotalCoupon = $mcProduct->SelectCountProduct($db);&nbsp; $lstTotalCoupon['total_coupons'] = count($lstTotalCoupon);?>所以你会得到这样的:echo $lstTotalCoupon['total_coupons'];

红糖糍粑

尝试返回SelectCountProduct()这样的:<?phpClass Product_MC {&nbsp; &nbsp;public function SelectCountProduct($db) {&nbsp; &nbsp; $stmt = $db->prepare(" SELECT * FROM product ");&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);&nbsp; &nbsp; $rows['total_coupons'] = $stmt->num_rows;&nbsp; &nbsp; return $rows;&nbsp; }}?>

慕斯王

修改您的查询以包含唯一 ID 的计数:SELECT&nbsp;count(id)&nbsp;AS&nbsp;total_coupons&nbsp;FROM&nbsp;product这将比大多数答案更有效,因为它是在数据库引擎中完成的。
打开App,查看更多内容
随时随地看视频慕课网APP