1
 $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();

就是这个。

4

2 回答 2

5

我是这样想的:

$result = mysqli_query($con, "SELECT * FROM `TableName` ORDER BY `PID` DESC LIMIT 1");
$row = mysqli_fetch_array($result);
$pidmax=$row['PID'];
于 2014-04-10T23:24:44.980 回答
2

您仍然需要调用 fetch,因为 max 仅在该点之后可用。见文档: http: //php.net/manual/en/mysqli-stmt.bind-result.php

$result->bind_result($max);

/* fetch values */
while ($result->fetch()) {
    printf("Max ID %i\n", $max);
}

$result->close();
于 2012-06-10T03:35:45.197 回答