$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 ,你们可以向我解释如何达到结果吗?
更新
在控制台模式下,这样的员工效果很好。 field id
是int和incremental(作为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();
就是这样。
答案 0 :(得分:3)
我这样想:
$result = mysqli_query($con, "SELECT * FROM `TableName` ORDER BY `PID` DESC LIMIT 1");
$row = mysqli_fetch_array($result);
$pidmax=$row['PID'];
答案 1 :(得分:2)
您仍然需要调用fetch,因为max仅在该点之后可用。请参阅doc: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();