如何使用mysqli获取总行数

2021-12-30 00:00:00 row count mysql

我正在尝试了解 mysqli 扩展并使用谷歌搜索,但除了 php.net 有用之外,关于此的信息很少.

i am trying to understand mysqli extension and did google but got very few info on this except php.net which was helpful.

现在,我正在尝试使用如下的 mysql 扩展实现我所能实现的目标:

now after all this i am trying to achieve what i could with mysql extension which is as follows:

// MYSQL STYLE OF fetching array, query limit and perform total row count all at once

$sql = "SELECT SQL_CALC_FOUND_ROWS *, post.id as pid, bla bla FROM account ORDER BY pid ASC". $eb["array"]['querylimit'];

$result = mysql_query($sql, $eb["con"]);
$TotalRcount = mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()"));

// Performing record count [current]
// $RecordCount = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
    // read columns
}

使用 mysqli 我怎样才能做到这一点?我确定我错过了很多东西.请帮助我举例说明如何实现我的目标.

with mysqli how can i achieve this? am sure i am missing many things. please help me with example on how to achieve my goal.

推荐答案

使用 mysqli 你可以按照以下方式进行(假设 mysqli 对象已经创建——你也可以使用 过程方法,略有不同):

using mysqli you do it the following way (assuming the mysqli object is already created -- you can also use the procedure methods, just slightly different):

$sql = "SELECT SQL_CALC_FOUND_ROWS *, post.id as pid, bla bla 
        FROM account ORDER BY pid ASC". $eb["array"]['querylimit'];
$result = $mysqli->query($sql);
$TotalRcount = $result->num_rows;
while($row=$result->fetch_assoc()){
    $col1 = $row['col1'];  // col1 is a placeholder for whatever column you are reading
    //read columns
}

相关文章