number of rows in a table

后端 未结 3 383
南笙
南笙 2020-12-10 21:01
 

        
相关标签:
3条回答
  • 2020-12-10 21:14

    conncet.php

    <?php
    class database
    {
      public $host = "localhost";
      public $user = "root";
      public $pass = "";
      public $db   = "db";
      public $link;
    
      public function __construct()
      {
         $this->connect();
      }
    
      private function connect()
      {
         $this->link = new mysqli($this->host, $this->user, $this->pass, $this->db);
         return $this->link;
      }
    
     public function select($query)
     {
        $result = $this->link->query($query) or die($this->link->error.__LINE__);
        if($result->num_rows > 0)
        {
          return $result;
        } 
        else 
        {
            return false;
        }
    }
    ?>
    

    Now action.php where query can be done as follows :

    <?php  include 'database.php'; ?>
     $db = new database();
     $query = "SELECT COUNT(*) FROM accounts";
     $row = $db->select($query)->num_rows;
     echo $row;
    ?>
    

    Full conncetion.php : link

    0 讨论(0)
  • 2020-12-10 21:17
    $query = "SELECT COUNT(*) FROM accounts";
    $result = mysqli_query($mysqli,$query);
    $rows = mysqli_fetch_row($result);
    echo $rows[0];
    

    or

    $query = "SELECT COUNT(*) AS SUM FROM accounts";
    $result = mysqli_query($mysqli,$query);
    $rows = mysqli_fetch_assoc($result);
    echo $rows['SUM'];
    
    0 讨论(0)
  • 2020-12-10 21:31

    What you ask for is pretty standard interaction with the Mysqli Result Object. One thing is the number of rows returned, and the other thing is to get the actual count value:

    $query  = "SELECT COUNT(*) FROM accounts";
    $result = $mysqli->query($query);
    
    echo 'Number of rows: ', $result->num_rows, "\n"; // 1 in your case
    
    list($count) = $result->fetch_row();
    
    echo 'COUNT(*): ', $count, "\n"; // the value from the database
    
    0 讨论(0)
提交回复
热议问题