获取行数和显示

时间:2013-12-09 20:32:42

标签: php count row

如何通过php获取数据库中的行总数并显示它?我在phpmyadmin中使用COUNT(column_name)FROM(table_name)完成了它,但是在使用php时我感到困惑。对不起我的愚蠢问题

2 个答案:

答案 0 :(得分:0)

尝试做这样的事情:

 <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

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

    if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {

        /* determine number of rows result set */
        $row_cnt = $result->num_rows;

        printf("Result set has %d rows.\n", $row_cnt);

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

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

答案 1 :(得分:0)

<?php    
$sql = mysql_query("SELECT * FROM `table`") or die(mysql_error());

    echo mysql_num_rows($sql);
?>