简体   繁体   English

MySQL - 计算 php 中的总行数

[英]MySQL - count total number of rows in php

What is the best MySQL command to count the total number of rows in a table without any conditions applied to it?什么是最好的 MySQL 命令来计算表中的总行数而不应用任何条件? I'm doing this through php, so maybe there is a php function which does this for me?我通过 php 这样做,所以也许有一个 php function 是为我做的? I don't know.我不知道。 Here is an example of my php:这是我的 php 的示例:

<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("some command");
$row = mysql_fetch_array($result);

mysql_close($con);
?>
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);

$total = $row[0];
echo "Total rows: " . $total;

mysql_close($con);
?>

Either use COUNT in your MySQL query or do a SELECT * FROM table and do:在您的 MySQL 查询中使用 COUNT 或执行 SELECT * FROM 表并执行以下操作:

$result = mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";

you can do it only in one line as below:您只能在一行中执行此操作,如下所示:

$cnt = mysql_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE"));
echo $cnt;

Use COUNT in a SELECT query.SELECT查询中使用COUNT

$result = mysql_query('SELECT COUNT(1) FROM table');
$num_rows = mysql_result($result, 0, 0);

for PHP 5.3 using PDO对于 PHP 5.3 使用 PDO

<?php
    $staff=$dbh->prepare("SELECT count(*) FROM staff_login");
    $staff->execute();
    $staffrow = $staff->fetch(PDO::FETCH_NUM);
    $staffcount = $staffrow[0];


    echo $staffcount;
?>
<?php
$conn=mysqli_connect("127.0.0.1:3306","root","","admin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('user_id') from login_user";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo "$row[0]";
mysqli_close($conn);
?>

Still having problem visit my tutorial http://www.studentstutorial.com/php/php-count-rows.php仍然有问题访问我的教程http://www.studentstutorial.com/php/php-count-rows.php

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  echo "number of rows: ",$rowcount;
  // Free result set
  mysqli_free_result($result);
  }

mysqli_close($con);
?>

it is best way (I think) to get the number of special row in mysql with php.最好的方法(我认为)用 php 获得 mysql 中的特殊行数。

mysqli_num_rows is used in php 5 and above. mysqli_num_rows用于 php 5 及以上版本。

eg例如

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";

if ($result=mysqli_query($con,$sql))
  {
  // Return the number of rows in result set
  $rowcount=mysqli_num_rows($result);
  printf("Result set has %d rows.\n",$rowcount);
  // Free result set
  mysqli_free_result($result);
  }
mysqli_close($con);
?>

use num_rows to get correct count for queries with conditions使用 num_rows 获取有条件的查询的正确计数

$result = $connect->query("select * from table where id='$iid'");
$count=$result->num_rows;
echo "$count";
$sql = "select count(column_name) as count from table";

Well, I used the following approach to do the same: I have to get a count of many tables for listing the number of services, projects, etc on the dashboard.好吧,我使用以下方法来做同样的事情:我必须计算许多表的数量,以便在仪表板上列出服务、项目等的数量。 I hope it helps.我希望它有所帮助。

PHP Code PHP 代码

// create a function 'cnt' which accepts '$tableName' as the parameter.

 function cnt($tableName){
        global $conection;
        $itemCount = mysqli_num_rows(mysqli_query($conection, "SELECT * FROM `$tableName`"));
        echo'<h6>'.$itemCount.'</h6>';
    }

Then when I need to get the count of items in the table, I call the function like following然后当我需要获取表中的项目数时,我调用 function,如下所示

<?php
  cnt($tableName = 'projects');
?>

In my HTML front end, so it renders the count number在我的 HTML 前端,它呈现计数

  • It's to be noted that I create the cnt() function as a global function in a separate file which I include in my head, so I can call it from anywhere in my code.需要注意的是,我将cnt() function 作为全局 function 创建在一个包含在我头脑中的单独文件中,因此我可以从代码中的任何位置调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM