php - 使用 MySql 和 PHP 时获取记录集中最后一条记录的值

标签 php mysql pdo

当使用 PHP 和 MySql 选择显示数据时,如何获取记录集中最后一行的值? MySql 或 Php 中是否有内置函数来执行此操作?

例如(使用 PDO):

select id from table limit 5;

Output:
1
2
3
4
5 -> How can I get this value to pass to a function, all things being the same

 while( $row = $stmt->fetch() ) {
   echo $row['id'];

}
 // The value of id of the last row i.e 5 needs to go into a function
       functionName(id value of last row goes here)

如何做到这一点?

最佳答案

   $last_id = 0
    while( $row = $stmt->fetch() ) {
       echo $row['id'];
       $last_id = $row['id'];
    }
     functionName($last_id)

关于php - 使用 MySql 和 PHP 时获取记录集中最后一条记录的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19024895/

相关文章:

php - 将csv文件直接导入phpmyadmin

php - 当外部存储库不再存在时,Composer/Laravel 从现有文件安装包

php - 我怎么能隐藏#!在浏览器地址栏上?

php - PDO::PARAM_* 与强制转换

mysql - DRUPAL PDO异常 : SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

php - 使用别名加入 Zend Framework

mysql - 选择两个没有连接的表mysql

mysql - 如何准备文件中的数据并将其插入到 Mysql DB 中?

mysql - 将Foxpro程序转换为MySQL存储过程

mysql - SQL JOIN 查询以收集两列的数据