$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/