php - MySQLi 查询以获取表中的最大 id?

 $mysqli = new mysqli("localhost","root","","mydatabase");

 if ($result = $mysqli->prepare("SELECT MAX(`id`) AS `id` FROM `mytable` WHERE `row1`=? OR `row2`=?"))
 {

    $id = 2;
    $result->bind_param("ii",$id,$id);
    $result->execute();
    $result->bind_result($max);
    $result->close();

    var_dump($max);

 }

 $mysqli->close();

不幸的是这段代码总是显示NULL,你能解释一下如何得到结果吗?

更新:

在控制台模式下,像这样的员工工作得很好。字段 id 是 int 和增量的(作为 PRIMARY INDEX),其他字段只是具有不同 int 值的行,我无法更改任何内容。

更新:

好吧,看来我找到了解决方案:

 $mysqli = new mysqli("localhost","root","","mydatabase");

 if ($result = $mysqli->prepare("SELECT MAX(`id`) AS `id` FROM `mytable` WHERE `row1`=? OR `row2`=?"))
 {

    $id = 2;
    $result->bind_param("ii",$id,$id);
    $result->execute();
    $obj = $result->get_result()->fetch_object();
    $max = $obj->id;
    $result->close();

    var_dump($max);

 }

 $mysqli->close();

就是这样。

最佳答案

我是这样想的:

$result = mysqli_query($con, "SELECT * FROM `TableName` ORDER BY `PID` DESC LIMIT 1");
$row = mysqli_fetch_array($result);
$pidmax=$row['PID'];

https://stackoverflow.com/questions/10966095/

相关文章:

email - 使用 ssl 的 Yandex smtp 设置

groovy - Groovy 中的 JUnit @Before 方法排序

sql - 存储过程返回的别名列 [SQL Server 2008]

sql-server-2008 - 一个选择语句中的不同条件

variables - Makefile 字符串变量

string - 将字符串/字符转换为 uint8

css - 向右浮动和小时对齐

sql - 如何优化此 SQL 查询(使用索引)?

regex - 检查目录是否存在递归地反转路径

caching - 什么是暖缓存?