使用php计算表中的行数 [英] count number of rows in table using php

查看:142
本文介绍了使用php计算表中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想计算已使用php在数据库中创建的表中的行数。我使用mysqli()。我需要表中的行数作为输出。

I just want to count the number of rows in a table that already created in a database using php. I used mysqli(). I need the number of rows in the table as the output.

推荐答案

 <?php
  $mysqli = new mysqli("hostname", "dbusername", "dbpassword", "dbname");

   /* check connection */
   if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
     exit();
   }

  if ($result = $mysqli->query("SELECT count(*) cc FROM tablename")) {

     /* fetch the first row as result */
     $row = $result->fetch_assoc();

    printf("Result set has %d rows.\n", $row['cc']);

   /* close result set */
    $result->close();
 }

 /* close connection */
 $mysqli->close();
?>

其实这是一个常见的问题,你可以在任何地方找到答案。

In fact it's a common question you can find the answer anywhere.

例如, http://php.net/手动/ en / mysqli-result.num-rows.php

您可以将此问题分为两个

You could separate this problem in to two


  1. 我想知道如何连接到mysql。

  2. 我想知道如何写sql指令。

这篇关于使用php计算表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆