dse323222 2014-03-11 15:23
浏览 70
已采纳

如何用php获取mysql数据库的行数

i would like to get the number of rows of a mysql database table with one single statement or a function

include "opendatabase.php"; //opens database

while (NUMBEROFROWS > 0){
    //do something
}

the NUMBEROFROWS should be replaced with the statement that returns the number of rows

i already tried to create a function

function getRowNumber(){
    $query = "SELECT COUNT(*) FROM  `votes`";
    $result = mysql_query($query, $connect);

    list($length) = mysql_fetch_row($result);
    return $length;

}

but it does not work if i dont put the include "opendatabase.php"; in it.

what am i doing wrong

  • 写回答

4条回答 默认 最新

  • dongmi5020 2014-03-11 16:39
    关注
    $result = mysql_query("SELECT * FROM tablename");
    
    if (mysql_num_rows($result) > 0) {
        // rows found..
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?