MySQL - 计算php中的总行数

时间:2011-07-11 19:47:50

标签: php mysql

在没有应用任何条件的情况下,计算表中总行数的最佳MySQL命令是什么?我是通过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);
?>

9 个答案:

答案 0 :(得分:42)

<?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);
?>

答案 1 :(得分:16)

在MySQL查询中使用COUNT或执行SELECT * FROM表并执行:

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

答案 2 :(得分:7)

你只能在一行中完成,如下所示:

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

答案 3 :(得分:6)

COUNT查询中使用SELECT

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

答案 4 :(得分:2)

for 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;
?>

答案 5 :(得分:2)

<table border="1" width="100%">
  <thead>
    <tr>
      <th style="width:300px">Product</th>
      <th>Barcode</th>
      <th>Stores</th>
      <th class="middle">Quantity</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>
      Item 1
      </td>
      <td>12345</td>
      <td colspan="3">
        <table border="1" width="100%">
          <tbody>
            <tr>
              <td>Store Name 1</td>
              <td class="middle">4</td>
            </tr>
            <tr>
              <td>Store Name 2</td>
              <td class="middle">4</td>
            </tr>
            <tr>
              <td>Store Name 3</td>
              <td class="middle">4</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

仍有问题请访问我的教程http://www.studentstutorial.com/php/php-count-rows.php

答案 6 :(得分:2)

mysqli_num_rows用于php 5及更高版本。

例如

<?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);
?>

答案 7 :(得分:0)

$sql = "select count(column_name) as count from table";

答案 8 :(得分:0)

使用两种方法对数据库表进行计数。您可以使用count()函数和mysqli_num_rows()函数。

$sql="select count('1') from users";

$rows_count_value = mysqli_num_rows($mysqliStatus);

您可以Count table rows by two methods